select id,sum(金额) from 表名 where datepart(month,日期时间)<=3 group by id union all select id,sum(金额) from 表名 where datepart(month,日期)>3 and datepart(month,日期)<=6 group by id union all select id,sum(金额) from 表名 where datepart(month,日期)>6 and datepart(month,日期)<=9 group by id union all select id,sum(金额) from 表名 where datepart(month,日期)>9 and datepart(month,日期)<=12 group by id
楼上的二位老大啊,不是用id进行group啊,ID是自增的啊,,,是主键
SELECT datename(qq,[date]),sum([money]) FROM moneytb GROUP BY datename(qq,[date])
select 第一季度=sum(case when datepart(month,日期)<=3 then 金额 else 0 end), 第二季度=sum(case when datepart(month,日期)>3 and datepart(month,日期)<=6 then 金额 else 0 end), 第三季度=sum(case when datepart(month,日期)>6 and datepart(month,日期)<=9 then 金额 else 0 end), 第四季度=sum(case when datepart(month,日期)>9 and datepart(month,日期)<=12 then 金额 else 0 end) from tab group by convert(char(7),日期,21)
--tryselect 一季=sum(case when datepart(quarter, 日期)=1 then 金额 else 0 end), 二季=sum(case when datepart(quarter, 日期)=2 then 金额 else 0 end), 三季=sum(case when datepart(quarter, 日期)=3 then 金额 else 0 end), 四季=sum(case when datepart(quarter, 日期)=4 then 金额 else 0 end) from T
where datepart(month,日期时间)<=3
group by id
union all
select id,sum(金额) from 表名
where datepart(month,日期)>3 and datepart(month,日期)<=6
group by id
union all
select id,sum(金额) from 表名
where datepart(month,日期)>6 and datepart(month,日期)<=9
group by id
union all
select id,sum(金额) from 表名
where datepart(month,日期)>9 and datepart(month,日期)<=12
group by id
FROM moneytb
GROUP BY datename(qq,[date])
第二季度=sum(case when datepart(month,日期)>3 and datepart(month,日期)<=6 then 金额 else 0 end),
第三季度=sum(case when datepart(month,日期)>6 and datepart(month,日期)<=9 then 金额 else 0 end),
第四季度=sum(case when datepart(month,日期)>9 and datepart(month,日期)<=12 then 金额 else 0 end)
from tab
group by convert(char(7),日期,21)
一季=sum(case when datepart(quarter, 日期)=1 then 金额 else 0 end),
二季=sum(case when datepart(quarter, 日期)=2 then 金额 else 0 end),
三季=sum(case when datepart(quarter, 日期)=3 then 金额 else 0 end),
四季=sum(case when datepart(quarter, 日期)=4 then 金额 else 0 end)
from T