MS SQL Server を使うようになったのでメモ。
サンプルデータの WideWorldImportersDW スキーマを使って T-SQL を覚える。
Fact.Sale から日別受注金額と7日間移動平均を抽出するクエリ
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];
結果はこうなる。
![query results](https://www.kwonline.org/memo2/wp-content/uploads/2021/11/tsql-2021-11-02.png)
日別受注と7日移動平均
PowerBI で表示するとこうなる。
![power bi](https://www.kwonline.org/memo2/wp-content/uploads/2021/11/pbi-2021-11-02.png)
PowerBI で表示
参考図書
ビッグデータ分析・活用のためのSQLレシピ
SQL Server 2016の教科書 開発編