答案 0 :(得分:1)
如果您只想在整周数据集中查找星期几的分组,则可以使用以下查询:
select
wk.dow [day of week]
, d.rt [day Total]
from
(select datename(weekday, createdon) dow
, sum(qty) rt
from table d
group by datename(weekday, createdon)) d
right join (select 'Monday' dow
union all
select 'Tuesday'
union all
select 'Wednesday'
union all
select 'Thursday'
union all
select 'Friday'
union all
select 'Saturday'
union all
select 'Sunday') wk on d.dow = wk.dow
虽然我有一种感觉,你正在寻找这个问题:
select datepart(week, createdon) [week nr]
, datename(weekday, createdOn) [day of week]
, dd.rt [running total]
from table d
cross apply (select sum(qty) rt from table dd where dd.createdOn <= d.createdOn) dd
order by createdon
, datepart(week, createdon)
答案 1 :(得分:1)
for 2012 version
SELECT FORMAT(B,'dddd') , sum(a) as toalamount
FROM #A
GROUP BY FORMAT(B,'dddd')
代替你需要用createdon和a替换数量 那么你将得到以上结果