回答編集履歴
3
typo
answer
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
create table cols(col varchar(10));
|
25
25
|
insert into cols values('sales'),('budget'),('ratio');
|
26
26
|
|
27
|
-
``
|
27
|
+
```
|
28
28
|
|
29
29
|
- とりあえず結合
|
30
30
|
```SQL
|
@@ -40,6 +40,7 @@
|
|
40
40
|
```
|
41
41
|
|
42
42
|
- 上記をもとに集計
|
43
|
+
|
43
44
|
```SQL
|
44
45
|
select col
|
45
46
|
,sum(1Q*val) as 1Q
|
2
修正
answer
CHANGED
@@ -8,23 +8,24 @@
|
|
8
8
|
|
9
9
|
- テーブル作成
|
10
10
|
```SQL
|
11
|
+
/* 元データ */
|
11
12
|
create table tbl(term varchar(10),team varchar(10),sales int, budget int,ratio double);
|
12
13
|
insert into tbl values
|
13
14
|
('1Q','teamA',100,200,0.5),
|
14
15
|
('2Q','teamA',100,300,0.3333),
|
15
16
|
('3Q','teamA',100,300,0.3333),
|
16
17
|
('4Q','teamA',100,200,0.5);
|
17
|
-
```
|
18
18
|
|
19
|
-
|
19
|
+
/* pivot作成 */
|
20
|
-
```SQL
|
21
|
-
create table pivot(
|
20
|
+
create table pivot(term varchar(10),1Q int,2Q int,3Q int,4Q int);
|
22
|
-
insert into pivot
|
23
|
-
select
|
21
|
+
insert into pivot select term,term='1Q' as 1q,term='2Q' as 2q,term='3Q' as 3q,term='4Q' as 4q from tbl;
|
24
|
-
union all select 'budget',term,term='1Q' as 1q,term='2Q' as 2q,term='3Q' as 3q,term='4Q' as 4q from tbl
|
25
|
-
union all select 'ratio',term,term='1Q' as 1q,term='2Q' as 2q,term='3Q' as 3q,term='4Q' as 4q from tbl
|
26
|
-
```
|
27
22
|
|
23
|
+
/* 横方向のデータ保持 */
|
24
|
+
create table cols(col varchar(10));
|
25
|
+
insert into cols values('sales'),('budget'),('ratio');
|
26
|
+
|
27
|
+
``
|
28
|
+
|
28
29
|
- とりあえず結合
|
29
30
|
```SQL
|
30
31
|
select
|
@@ -34,10 +35,11 @@
|
|
34
35
|
when 'budget' then t2.budget
|
35
36
|
when 'ratio' then t2.ratio
|
36
37
|
end as val
|
38
|
+
from (select * from pivot,cols) as t1 inner join
|
37
|
-
|
39
|
+
tbl as t2 on t1.term =t2.term ;
|
38
40
|
```
|
39
41
|
|
40
|
-
- 集計
|
42
|
+
- 上記をもとに集計
|
41
43
|
```SQL
|
42
44
|
select col
|
43
45
|
,sum(1Q*val) as 1Q
|
@@ -52,9 +54,10 @@
|
|
52
54
|
when 'budget' then t2.budget
|
53
55
|
when 'ratio' then t2.ratio
|
54
56
|
end as val
|
57
|
+
from (select * from pivot,cols) as t1 inner join
|
55
|
-
|
58
|
+
tbl as t2 on t1.term =t2.term
|
56
59
|
) sub
|
57
|
-
group by col
|
60
|
+
group by col;
|
58
61
|
|
59
62
|
```
|
60
63
|
|
1
sample
answer
CHANGED
@@ -2,4 +2,61 @@
|
|
2
2
|
基本的に1Q~4Qのように型が違うもの(おそらくintとdouble)を
|
3
3
|
一緒に表示するのはデータの持ち方としておかしいと思います
|
4
4
|
|
5
|
-
また命題のデータはteamが縦横変換されておらずピボットになっていません。
|
5
|
+
また命題のデータはteamが縦横変換されておらずピボットになっていません。
|
6
|
+
|
7
|
+
# pivotを使ったsample
|
8
|
+
|
9
|
+
- テーブル作成
|
10
|
+
```SQL
|
11
|
+
create table tbl(term varchar(10),team varchar(10),sales int, budget int,ratio double);
|
12
|
+
insert into tbl values
|
13
|
+
('1Q','teamA',100,200,0.5),
|
14
|
+
('2Q','teamA',100,300,0.3333),
|
15
|
+
('3Q','teamA',100,300,0.3333),
|
16
|
+
('4Q','teamA',100,200,0.5);
|
17
|
+
```
|
18
|
+
|
19
|
+
- pivot作成
|
20
|
+
```SQL
|
21
|
+
create table pivot(col varchar(10),term varchar(10),1Q int,2Q int,3Q int,4Q int);
|
22
|
+
insert into pivot
|
23
|
+
select 'sales',term,term='1Q' as 1q,term='2Q' as 2q,term='3Q' as 3q,term='4Q' as 4q from tbl
|
24
|
+
union all select 'budget',term,term='1Q' as 1q,term='2Q' as 2q,term='3Q' as 3q,term='4Q' as 4q from tbl
|
25
|
+
union all select 'ratio',term,term='1Q' as 1q,term='2Q' as 2q,term='3Q' as 3q,term='4Q' as 4q from tbl
|
26
|
+
```
|
27
|
+
|
28
|
+
- とりあえず結合
|
29
|
+
```SQL
|
30
|
+
select
|
31
|
+
t1.*,
|
32
|
+
case t1.col
|
33
|
+
when 'sales' then t2.sales
|
34
|
+
when 'budget' then t2.budget
|
35
|
+
when 'ratio' then t2.ratio
|
36
|
+
end as val
|
37
|
+
from pivot as t1 inner join tbl as t2 on t1.term =t2.term
|
38
|
+
```
|
39
|
+
|
40
|
+
- 集計
|
41
|
+
```SQL
|
42
|
+
select col
|
43
|
+
,sum(1Q*val) as 1Q
|
44
|
+
,sum(2Q*val) as 2Q
|
45
|
+
,sum(3Q*val) as 3Q
|
46
|
+
,sum(4Q*val) as 4Q
|
47
|
+
from(
|
48
|
+
select
|
49
|
+
t1.*,
|
50
|
+
case t1.col
|
51
|
+
when 'sales' then t2.sales
|
52
|
+
when 'budget' then t2.budget
|
53
|
+
when 'ratio' then t2.ratio
|
54
|
+
end as val
|
55
|
+
from pivot as t1 inner join tbl as t2 on t1.term =t2.term
|
56
|
+
) sub
|
57
|
+
group by col
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
※前回も下記来ましたが結果としてteamを縦に書くのはロジックとしておかしいので
|
62
|
+
やっていません。
|