numberofCalls、Date、CallsNotRecieved などの列を持つテーブルがあります。
このレコードを 1 か月間取得する方法が必要ですが、毎週土曜日の終わりに週の合計を表示する必要があり、結局、その月の総合計を表示する必要があります。
SQLサーバーで可能ですか?ヘルプはありますか?
numberofCalls、Date、CallsNotRecieved などの列を持つテーブルがあります。
このレコードを 1 か月間取得する方法が必要ですが、毎週土曜日の終わりに週の合計を表示する必要があり、結局、その月の総合計を表示する必要があります。
SQLサーバーで可能ですか?ヘルプはありますか?
データ構造に関する詳細情報を提供してください。列[Date]
のタイプがdatetime
であり、日付/週の合計/月の合計をすべて 1 つの列にまとめるとします。
set datefirst 7
select
case
when grouping(cast(datepart(week, [Date]) as varchar(255)))=1 then '<monthtotal>'
when grouping(cast([Date] as date))=1 then '<weektotal>'
else cast(cast([Date] as date) as varchar(255))
end as Period
,CallsNotRecieved = sum(CallsNotRecieved)
,NumberOfCalls = sum(numberofCalls)
from <yourtable>
group by
grouping sets(
(cast(datepart(month, [Date]) as varchar(255)), cast(datepart(week, [Date]) as varchar(255)),cast([Date] as date)),
(cast(datepart(month, [Date]) as varchar(255)), cast(datepart(week, [Date]) as varchar(255))),
(cast(datepart(month, [Date]) as varchar(255)))
)
を使用せずにこれを行う方法を理解しset datefirst
、weektotal と monthtotal の代わりに週/月番号を挿入し、 を使用してデータ セットが順序付けられていることを確認することもできますorder by grouping()
。order by [Period]
編集:SQL Server 2005では、 がないため、 (またはさらに悪い - )句grouping sets
を使用し、不要なグループ化の組み合わせを除外する必要がありますWITH ROLLUP
WITH CUBE
grouping(<column_name>)=1