我有包含用户数据的数据输入表(ServiceData)。列是Userid,BillDate(类型DateTime)等。 我需要每周(当前一周)获取用户数。
查询
select BillDate
,DATENAME(dw,BillDate) as day
,count(BillDate) as total
from ServiceData
group by
BillDate,DATENAME(dw,BillDate)
order by BillDate desc
这只是获取日期,但是我想获取星期日入场计数,星期一入场计数....从星期日到星期六(当前每周)是可能的吗?
预期产量
ID | TOTAL | DAY
--------------------------
1 | 23 | Sun
2 | 54 | Mon
3 | 17 | Tues
4 | 56 | Thus
5 | 45 | Fri
6 | 78 | Sat
答案 0 :(得分:1)
这应该可以解决问题:
SELECT
row_number() over (order by (SELECT 1)) ID,
count(*) Total,
LEFT(Datename(weekday, Cast(Billdate as date)), 3) Day
FROM
ServiceData
WHERE
BillDate >= dateadd(week, datediff(d, -1, getdate()-2)/7, -1)
GROUP BY
Cast(Billdate as date)
ORDER BY
Cast(Billdate as date)