能否一條陳述句統計出天,周,月的資料?
select 名字,count(數量) as 月 from 表1 where datediff(month,A.STARTTIME,getdate())=0 group by 名字
select 名字,count(數量) as 周 from 表1 where datediff(week,A.STARTTIME,getdate())=0 group by 名字
select 名字,count(數量) as 日 from 表1 where datediff(dd,A.STARTTIME,getdate())=0 group by 名字
或者將上面結果合并成
名字 月 周 日
張三 100 50 10
李四 90 49 12
uj5u.com熱心網友回復:
SELECT 名字,SUM(CASE WHEN datediff(month,A.STARTTIME,getdate())=0 THEN 1 ELSE 0 END) AS 月,
SUM(CASE WHEN datediff(week,A.STARTTIME,getdate())=0 THEN 1 ELSE 0 END) AS 周,
SUM(CASE WHEN datediff(dd,A.STARTTIME,getdate())=0 THEN 1 ELSE 0 END) AS 日
FROM 表1
group by 名字
uj5u.com熱心網友回復:
為了提高運算效率,撰寫代碼方便,事先在資料庫表結構里增加好 月, 周 的欄位轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/12941.html
標籤:基礎類
