sql server 2008 - How do you align merged data in SQl -
i have 3 tables want combine final table in mssql2008. problem lies in merging data in final table format. issue number of columns have in each table close 100 each need efficient code creates final table
i need sql script generate final table
<h1> table 2008</h1> <table border = 1> <tr> <td> id </td> <td> sample_year </td> <td> total_at_t-5 </td> <td> total_at_t-4 </td> </tr> <tr> <td> mmm </td> <td> 2008 </td> <td> 5.25 </td> <td> 65.25</td> </tr> </table> <p> </p> <h1> table 2011</h1> <table header = "2011" border = 1> <tr> <td> id </td> <td> sample_year </td> <td> total_at_t-6 </td> <td> total_at_t-5 </td> </tr> <tr> <td> mmm </td> <td> 2011 </td> <td> 7.25 </td> <td> 98.25</td> </tr> </table> <p> </p> <h1> table 2013</h1> <table header = "2013" border = 1> <tr> <td> id </td> <td> sample_year </td> <td> total_at_t-7 </td> <td> total_at_t-6 </td> </tr> <tr> <td> mmm </td> <td> 2013 </td> <td> 3.25 </td> <td> 7.25</td> </tr> </table> <p> </p> <h1> table final</h1> <table hearder = "final table" border = 1> <tr> <td> id </td> <td> sample_year </td> <td> total_at_t-7 </td> <td> total_at_t-6 </td> <td> total_at_t-5 </td> <td> total_at_t-4 </td> </tr> <tr> <td> mmm </td> <td> null </td> <td> null </td> <td> null </td> <td> 5.25 </td> <td> 65025 </td> </tr> <tr> <td> mmm </td> <td> 2011 </td> <td> null </td> <td> 7.25</td> <td> 98.25 </td> <td> null </td> </tr> <tr> <td> mmm </td> <td> 2013 </td> <td> 3.25 </td> <td> 7.25 </td> <td> null </td> <td> null</td> </tr> </table>
just assume table
exists in sql database, if case, need use union
(http://www.w3schools.com/sql/sql_union.asp) join them together
declare @t2008 table (id varchar(3), sample_year int, [total_at_t-5] decimal(10, 2), [total_at_t-4] decimal(10, 2)) insert @t2008 values ('mmm', 2008, 5.25, 65.25) declare @t2011 table (id varchar(3), sample_year int, [total_at_t-6] decimal(10, 2), [total_at_t-5] decimal(10, 2)) insert @t2011 values ('mmm', 2011, 7.25, 98.25) declare @t2013 table (id varchar(3), sample_year int, [total_at_t-7] decimal(10, 2), [total_at_t-6] decimal(10, 2)) insert @t2013 values ('mmm', 2013, 3.25, 7.25) select id, sample_year, null [total_at_t-7], null [total_at_t-6], [total_at_t-5], [total_at_t-4] @t2008 union select id, sample_year, null, [total_at_t-6], [total_at_t-5], null @t2011 union select id, sample_year, [total_at_t-7], [total_at_t-6], null, null @t2013
Comments
Post a Comment