MySQLのソース文についてのご相談
table1
-----------------------
Date,PageTitle,Pageviews
-----------------------
2020/10/1,A,1
2020/10/2,A,1
2020/10/3,A,1
2020/10/4,A,1
2020/10/5,A,1
2020/10/6,A,1
2020/10/7,A,1
2020/10/8,A,1
2020/10/9,A,1
2020/10/10,A,1
2020/10/2,B,1
2020/10/3,B,1
2020/10/4,B,1
2020/10/5,B,1
2020/10/6,B,1
-----------------------
・実現したいこと
PageTitleごとにDateの最小値から7日以内のPageviewsを合計して以下のように出力したい
↓
-----------------------
Date(MIN),PageTitle,SUM(Pageviews)
-----------------------
2020/10/1,A,7
2020/10/2,B,5
-----------------------
これを実現するためにはどのようなSQL文を記述すれば可能でしょうか?
お分かりになる方、ご教授よろしくお願いいたします。
0
Best Answer
-
@tokupon さん
こんな感じで実現できるかと思います。
select a.`mindate` , a.`PageTitle` , sum(b.`Pageviews`) as `Pageviews` from ( select -- PageTitle毎の最小日を取得 min(`Date`) as mindate , `PageTitle` from `table1` group by `PageTitle` ) a left join `table1` b on a.`PageTitle` = b.`PageTitle` and b.`Date` < date_add(a.`mindate`, interval 7 day) -- 最小日の7日以内の条件で結合 group by a.`mindate` , a.`PageTitle`
1
Answers
-
ご回答ありがとうございます。
こちらの内容で実現できました。
助かります。1
Categories
- 7.7K All Categories
- 3 Connect
- 919 Connectors
- 244 Workbench
- 477 Transform
- 1.8K Magic ETL
- 60 SQL DataFlows
- 446 Datasets
- 37 Visualize
- 198 Beast Mode
- 2K Charting
- 8 Variables
- 1 Automate
- 348 APIs & Domo Developer
- 82 Apps
- Workflows
- 14 Predict
- 3 Jupyter Workspaces
- 11 R & Python Tiles
- 241 Distribute
- 59 Domo Everywhere
- 241 Scheduled Reports
- 15 Manage
- 36 Governance & Security
- 27 Product Ideas
- 1.1K Ideas Exchange
- Community Forums
- 14 Getting Started
- 1 Community Member Introductions
- 49 Community News
- 18 Event Recordings
- 579 日本支部