c# - How to group by and split columns dynamically with sql query? -
i have table contains rows below;
id date city income 1 2013 kansas 10$ 1 2013 kansas 15$ 1 2014 kansas 30$ 2 2013 chicago 50$ ...
i need such query transform this;
id date city income1 income2 1 2013 kansas 10$ 15$ 1 2014 kansas 30$ 2 2013 chicago 50$ ...
so should group id, date split income values different columns.. how try achieve thing not right;
select id, date, min(city), concat(income,',') [sheet 1$] group id, date
create table table1(id int, year int, city varchar(10), income int) insert table1 values(1, 2013, 'kansas', 10) insert table1 values(1, 2013, 'kansas', 10) insert table1 values(1, 2013, 'kansas', 15) insert table1 values(1, 2014, 'kansas', 30) insert table1 values(2, 2013, 'chicago', 50) select max(rn) maxcol from( select *, row_number() over(partition id,year order id) rn table1 )as tbl1 select * ( select *, row_number() over(partition id,year order id) rn table1 ) src pivot ( max(income) rn in ([1], [2], [3]) ) piv;
actually dynamic sql option when number of income value(count) per year not fixed.
wiki
Comments
Post a Comment