sql - How to select data based Group by ID,Year,Month in sqlserver? -


how data based on given below format:

id    name      year      month     amount  1              2012      jan       100 1              2012      jan       900  1              2012      jan       300 1              2012      apr       100 1              2012      apr       500  2       b        2013      may       100  

output in below mentioned form, if name, year, , month in parameter,

id     name   jan    feb  mar  apr  may  jun ...... jan .....may total 1            1300   0   0    600   0    0   .....  0 ...... 0 1900 2       b      0      0   0     0    0    0.........0.......100 100  

declare @t table (id int,name varchar(10),years varchar(10),months  varchar(10),amt int  ) insert  @t (id,name,years,months,amt)values (1,'a','2012','jan',100) insert  @t (id,name,years,months,amt)values (2,'a','2012','jan',100) insert  @t (id,name,years,months,amt)values (3,'a','2012','apr',200) insert  @t (id,name,years,months,amt)values (4,'a','2012','apr',100) insert  @t (id,name,years,months,amt)values (5,'b','2013','may',200)  select id,     name,     isnull(jan,0) jan,     isnull(feb,0) feb,     isnull(mar,0) mar,     isnull(apr,0)as apr,isnull(jun,0)as jun,isnull(jul,0)as jul,isnull(aug,0)as aug          (select distinct t.id,t.name,t.years,t.months months,t.amt          @t t)t pivot (sum(amt)for months in( [jan],     [feb],     [mar],     [apr],[jun],[jul],[aug]))p 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -