回答編集履歴
1
追加質問に対する回答を追記
test
CHANGED
@@ -43,3 +43,65 @@
|
|
43
43
|
GROUP BY T1.PC購入日
|
44
44
|
|
45
45
|
```
|
46
|
+
|
47
|
+
---
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
**2021/12/23 10:20 追記**
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
> 「その日に使われたPC経由の注文数」
|
56
|
+
|
57
|
+
> 「列の12月1日〜12月7日」
|
58
|
+
|
59
|
+
> をいれる方法はありますでしょうか?
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
やり方はいろいろあると思いますが、たとえば以下のようなSQLはどうでしょうか?
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
```SQL
|
68
|
+
|
69
|
+
SELECT
|
70
|
+
|
71
|
+
T1.PC購入日,
|
72
|
+
|
73
|
+
(SELECT COUNT(*) FROM tbl T3 WHERE T1.PC購入日 = T3.購入日 AND 購入端末 = 'PC') AS PC購入数,
|
74
|
+
|
75
|
+
COUNT(CASE WHEN T2.購入日 = '2021-12-01' THEN T1.userid END) AS "2021-12-01",
|
76
|
+
|
77
|
+
COUNT(CASE WHEN T2.購入日 = '2021-12-02' THEN T1.userid END) AS "2021-12-02",
|
78
|
+
|
79
|
+
COUNT(CASE WHEN T2.購入日 = '2021-12-03' THEN T1.userid END) AS "2021-12-03",
|
80
|
+
|
81
|
+
COUNT(CASE WHEN T2.購入日 = '2021-12-04' THEN T1.userid END) AS "2021-12-04",
|
82
|
+
|
83
|
+
COUNT(CASE WHEN T2.購入日 = '2021-12-05' THEN T1.userid END) AS "2021-12-05",
|
84
|
+
|
85
|
+
COUNT(CASE WHEN T2.購入日 = '2021-12-06' THEN T1.userid END) AS "2021-12-06",
|
86
|
+
|
87
|
+
COUNT(CASE WHEN T2.購入日 = '2021-12-07' THEN T1.userid END) AS "2021-12-07"
|
88
|
+
|
89
|
+
FROM (
|
90
|
+
|
91
|
+
SELECT userid, MIN(購入日) AS PC購入日
|
92
|
+
|
93
|
+
FROM tbl
|
94
|
+
|
95
|
+
WHERE 購入端末 = 'PC'
|
96
|
+
|
97
|
+
GROUP BY userid
|
98
|
+
|
99
|
+
) AS T1
|
100
|
+
|
101
|
+
JOIN tbl AS T2 ON T1.userid = T2.userid AND T1.PC購入日 < T2.購入日
|
102
|
+
|
103
|
+
WHERE T2.購入端末 = 'SP'
|
104
|
+
|
105
|
+
GROUP BY T1.PC購入日
|
106
|
+
|
107
|
+
```
|