MS SQL Server を使うようになったのでメモ。
サンプルデータの WideWorldImportersDW スキーマを使って T-SQL を覚える。
Fact.Sale から日別受注金額と7日間移動平均を抽出するクエリ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | SELECT [Invoice Date Key ] as sale_date, sum ([Total Including Tax]) as total_sale, avg ( sum ([Total Including Tax])) over( order by [Invoice Date Key ] rows between 6 preceding and current row ) as avg_seven_days FROM [WideWorldImportersDW].[Fact].[Sale] where [Invoice Date Key ] between '2013-01-01' and '2013-01-31' group by [Invoice Date Key ] order by [Invoice Date Key ]; |
結果はこうなる。

日別受注と7日移動平均
PowerBI で表示するとこうなる。

PowerBI で表示
参考図書
ビッグデータ分析・活用のためのSQLレシピ
SQL Server 2016の教科書 開発編