SQLについて質問です。
日毎に縦に数字が並ぶようなselect文が2つあり、それらを横につなげたいです。
以下のようなテーブルが2つあります。
table1
date | id | user_id |
---|---|---|
2022/1/1 | 1234 | 6 |
2022/1/1 | 1235 | 7 |
2022/1/2 | 1236 | 8 |
・・・ | ・・・ | ・・・ |
table2
date | user_id | subject |
---|---|---|
2022/1/1 | 6 | 1 |
2022/1/1 | 7 | 2 |
2022/1/2 | 8 | 3 |
・・・ | ・・・ | ・・・ |
この2つのテーブルを使い全体のidの数ととsubjectが2のidの数を日毎に並べたいです。
イメージ↓
date | 全体の申し込み数 | subjectが2のときの申込数 |
---|---|---|
2022/1/1 | 2 | 1 |
2022/1/2 | 3 | 2 |
table1.idの数を日毎に集計するselect文とtable2のsubjectが2のときのtable1.idの数を日毎に集計するselect文の2つは用意できました。しかしそこから横につなげることができないです。
1つ目のselect
select table1.date as day, count(table1.id) as 申込数 from table1 group by day
2つ目のselect
select table1.date as day, count(table1.id) as 申込数 from table1 left join table2 on table1.user_id = table2.user_id where table2.subject = 2 group by day
サブクエリなどいろいろ考えたのですが、日毎に集計したものを横に並べるというのが調べてもよく分かりませんでした。教えていただきたいです。宜しくおねがいします。
回答1件
あなたの回答
tips
プレビュー