有个表TT字段为YEAR varchar(10),account int,qrt varchar(10);其中qrt只是四种值(1,2,3,4)
我想取出所有的按YEAR分组的qrt值为1的,值为2,值为3,值为4的记录
也就是按YEAR,qrt1,qrt2,qrt3,qrt4要数据库里所有的记录,请高手们指点一下...........
我想取出所有的按YEAR分组的qrt值为1的,值为2,值为3,值为4的记录
也就是按YEAR,qrt1,qrt2,qrt3,qrt4要数据库里所有的记录,请高手们指点一下...........
select [year],
qrt1=sum(case qrt when '1' then 1 else 0 end),
qrt2=sum(case qrt when '2' then 1 else 0 end),
qrt3=sum(case qrt when '3' then 1 else 0 end),
qrt4=sum(case qrt when '4' then 1 else 0 end)
from tt group by [year]
qrt1=sum(case when qrt=1 then account else 0 end),
qrt2=sum(case when qrt=2 then account else 0 end),
qrt3=sum(case when qrt=3 then account else 0 end),
qrt4=sum(case when qrt=4 then account else 0 end)
from tt
group year
qrt1=sum(case when qrt=1 then account else 0 end),
qrt2=sum(case when qrt=2 then account else 0 end),
qrt3=sum(case when qrt=3 then account else 0 end),
qrt4=sum(case when qrt=4 then account else 0 end)
from tt
group by year