回答編集履歴
5
戻し
test
CHANGED
@@ -46,31 +46,41 @@
|
|
46
46
|
|
47
47
|
```SQL
|
48
48
|
|
49
|
+
with UA as (
|
50
|
+
|
51
|
+
select U.id, U.group_name, U.name, A.height
|
52
|
+
|
53
|
+
from countries as U
|
54
|
+
|
55
|
+
inner join players as A
|
56
|
+
|
57
|
+
on U.id = A.country_id
|
58
|
+
|
59
|
+
)
|
60
|
+
|
49
61
|
select group_name, name
|
50
62
|
|
51
|
-
|
63
|
+
, avg(height),
|
52
64
|
|
53
|
-
|
65
|
+
, max(height),
|
54
66
|
|
55
|
-
|
67
|
+
, sum(case when height <= 170 then 1 else 0 end) as under170
|
56
68
|
|
57
|
-
|
69
|
+
, group_avg_height
|
58
70
|
|
59
71
|
from (
|
60
72
|
|
61
|
-
|
73
|
+
SELECT *
|
62
74
|
|
63
|
-
|
75
|
+
, (select avg(height) from UA
|
64
76
|
|
65
|
-
|
77
|
+
where id=base.ID and group_name=base.group_name
|
66
78
|
|
67
|
-
|
79
|
+
) as group_avg_height
|
68
80
|
|
69
|
-
|
81
|
+
FROM UA as base
|
70
82
|
|
71
|
-
) U inner join players as A
|
72
|
-
|
73
|
-
|
83
|
+
) UA
|
74
84
|
|
75
85
|
GROUP by group_name, group_avg_height, name
|
76
86
|
|
4
推敲
test
CHANGED
@@ -14,9 +14,9 @@
|
|
14
14
|
|
15
15
|
select group_name, name
|
16
16
|
|
17
|
-
, avg(height)
|
17
|
+
, avg(height)
|
18
18
|
|
19
|
-
, max(height)
|
19
|
+
, max(height)
|
20
20
|
|
21
21
|
, sum(case when height <= 170 then 1 else 0 end) as under170
|
22
22
|
|
@@ -48,9 +48,9 @@
|
|
48
48
|
|
49
49
|
select group_name, name
|
50
50
|
|
51
|
-
, avg(height)
|
51
|
+
, avg(height)
|
52
52
|
|
53
|
-
, max(height)
|
53
|
+
, max(height)
|
54
54
|
|
55
55
|
, sum(case when height <= 170 then 1 else 0 end) as under170
|
56
56
|
|
3
推敲
test
CHANGED
@@ -46,18 +46,6 @@
|
|
46
46
|
|
47
47
|
```SQL
|
48
48
|
|
49
|
-
with UA as (
|
50
|
-
|
51
|
-
select U.id, U.group_name, U.name, A.height
|
52
|
-
|
53
|
-
from countries as U
|
54
|
-
|
55
|
-
inner join players as A
|
56
|
-
|
57
|
-
on U.id = A.country_id
|
58
|
-
|
59
|
-
)
|
60
|
-
|
61
49
|
select group_name, name
|
62
50
|
|
63
51
|
, avg(height),
|
@@ -70,17 +58,19 @@
|
|
70
58
|
|
71
59
|
from (
|
72
60
|
|
73
|
-
SELECT *
|
61
|
+
SELECT *
|
74
62
|
|
75
|
-
, (select avg(height) from
|
63
|
+
, (select avg(height) from players
|
76
64
|
|
77
|
-
where id=
|
65
|
+
where country_id=U.id
|
78
66
|
|
79
|
-
) as group_avg_height
|
67
|
+
) as group_avg_height
|
80
68
|
|
81
|
-
|
69
|
+
from countries U
|
82
70
|
|
71
|
+
) U inner join players as A
|
72
|
+
|
83
|
-
|
73
|
+
on U.id = A.country_id
|
84
74
|
|
85
75
|
GROUP by group_name, group_avg_height, name
|
86
76
|
|
2
推敲
test
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
|
25
25
|
from (
|
26
26
|
|
27
|
-
SELECT U.group_name, U.name, A.height
|
27
|
+
SELECT U.group_name, U.name, A.height
|
28
28
|
|
29
29
|
, avg(height) over(partition by group_name) as group_avg_height
|
30
30
|
|
1
追記
test
CHANGED
@@ -1 +1,89 @@
|
|
1
1
|
group_nameのみの集計を行ったものとjoinすれば良いと思います。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
### 追記
|
6
|
+
|
7
|
+
group_nameのみの集計を行うサンプルです
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
**Window関数**
|
12
|
+
|
13
|
+
```SQL
|
14
|
+
|
15
|
+
select group_name, name
|
16
|
+
|
17
|
+
, avg(height),
|
18
|
+
|
19
|
+
, max(height),
|
20
|
+
|
21
|
+
, sum(case when height <= 170 then 1 else 0 end) as under170
|
22
|
+
|
23
|
+
, group_avg_height
|
24
|
+
|
25
|
+
from (
|
26
|
+
|
27
|
+
SELECT U.group_name, U.name, A.height, A.country_id
|
28
|
+
|
29
|
+
, avg(height) over(partition by group_name) as group_avg_height
|
30
|
+
|
31
|
+
from countries as U
|
32
|
+
|
33
|
+
inner join players as A
|
34
|
+
|
35
|
+
on U.id = A.country_id
|
36
|
+
|
37
|
+
) UA
|
38
|
+
|
39
|
+
GROUP by group_name, group_avg_height, name
|
40
|
+
|
41
|
+
ORDER by group_name, group_avg_height DESC
|
42
|
+
|
43
|
+
```
|
44
|
+
|
45
|
+
**相関問合せ**
|
46
|
+
|
47
|
+
```SQL
|
48
|
+
|
49
|
+
with UA as (
|
50
|
+
|
51
|
+
select U.id, U.group_name, U.name, A.height
|
52
|
+
|
53
|
+
from countries as U
|
54
|
+
|
55
|
+
inner join players as A
|
56
|
+
|
57
|
+
on U.id = A.country_id
|
58
|
+
|
59
|
+
)
|
60
|
+
|
61
|
+
select group_name, name
|
62
|
+
|
63
|
+
, avg(height),
|
64
|
+
|
65
|
+
, max(height),
|
66
|
+
|
67
|
+
, sum(case when height <= 170 then 1 else 0 end) as under170
|
68
|
+
|
69
|
+
, group_avg_height
|
70
|
+
|
71
|
+
from (
|
72
|
+
|
73
|
+
SELECT *
|
74
|
+
|
75
|
+
, (select avg(height) from UA
|
76
|
+
|
77
|
+
where id=base.ID and group_name=base.group_name
|
78
|
+
|
79
|
+
) as group_avg_height
|
80
|
+
|
81
|
+
FROM UA as base
|
82
|
+
|
83
|
+
) UA
|
84
|
+
|
85
|
+
GROUP by group_name, group_avg_height, name
|
86
|
+
|
87
|
+
ORDER by group_name, group_avg_height DESC
|
88
|
+
|
89
|
+
```
|