大家好,請問有誰會解這一題的SQL,我想了好久還是想不出來
CREATE TABLE A (
XYEAR INT,
Q1 INT,
Q2 INT,
Q3 INT,
Q4 INT);
INSERT INTO A
VALUES
(2017,200,250,235,266),
(2018,195,220,215,240);
SELECT XYEAR,'Q1' AS 'QQ',MAX(Q1) AS 'AMOUNT'
FROM A
GROUP BY XYEAR
UNION
SELECT XYEAR,'Q2' AS 'QQ',MAX(Q2) AS 'AMOUNT'
FROM A
GROUP BY XYEAR
UNION
SELECT XYEAR,'Q3' AS 'QQ',MAX(Q3) AS 'AMOUNT'
FROM A
GROUP BY XYEAR
UNION
SELECT XYEAR,'Q4' AS 'QQ',MAX(Q4) AS 'AMOUNT'
FROM A
GROUP BY XYEAR
ORDER BY XYEAR,QQ
謝謝您的回覆,請問一下,如果是顛倒過來由下面的表格轉成上面的表格SQL又該如何下?
CREATE TABLE X (
XYEAR INT,
QQ VARCHAR(10),
AMOUNT INT);
INSERT INTO X
VALUES
(2017,'Q1',200),
(2017,'Q2',250),
(2017,'Q3',235),
(2017,'Q4',266),
(2018,'Q1',195),
(2018,'Q2',220),
(2018,'Q3',215),
(2018,'Q4',240);
SELECT A.XYEAR,A.AMOUNT AS 'Q1',B.AMOUNT AS 'Q2',C.AMOUNT AS 'Q3',D.AMOUNT AS 'Q4'
FROM X AS A
LEFT JOIN X AS B ON B.XYEAR=A.XYEAR AND B.QQ='Q2'
LEFT JOIN X AS C ON C.XYEAR=A.XYEAR AND C.QQ='Q3'
LEFT JOIN X AS D ON D.XYEAR=A.XYEAR AND D.QQ='Q4'
WHERE A.QQ='Q1'
ORDER BY A.XYEAR
可以請教大大是怎麼練習SQL嗎?總覺這種類型的SQL題目都好活,腦袋需要轉一下,不然根本想不出來
怎麼練習SQL ?
上it邦幫忙==>右上方的"放大鏡"查"邦友"==>一級屠豬士,暐翰,純真的人....... 解SQL都很高明.
學SQL沒甚麼絕招,就多看看別人怎麼分析問題,出哪一招.
使用PostgreSQL
create table it0422 (
xyear int not null
, q1 int not null
, q2 int not null
, q3 int not null
, q4 int not null
);
insert into it0422 values
(2017,200,250,235,266),
(2018,195,220,215,240);
select xyear as "年度"
, unnest(array['q1', 'q2', 'q3', 'q4']) as "各季"
, unnest(array[q1, q2, q3, q4]) as "購買金額"
from it0422
order by 1,2;
年度 | 各季 | 購買金額
------+------+----------
2017 | q1 | 200
2017 | q2 | 250
2017 | q3 | 235
2017 | q4 | 266
2018 | q1 | 195
2018 | q2 | 220
2018 | q3 | 215
2018 | q4 | 240
(8 rows)
create table it0422x as
select xyear
, unnest(array['q1', 'q2', 'q3', 'q4']) as quarter
, unnest(array[q1, q2, q3, q4]) as qty
from it0422
order by 1,2;
select *
from it0422x;
xyear | quarter | qty
-------+---------+-----
2017 | q1 | 200
2017 | q2 | 250
2017 | q3 | 235
2017 | q4 | 266
2018 | q1 | 195
2018 | q2 | 220
2018 | q3 | 215
2018 | q4 | 240
(8 rows)
select xyear as year
, min(qty) filter (where quarter = 'q1') as q1
, min(qty) filter (where quarter = 'q2') as q2
, min(qty) filter (where quarter = 'q3') as q3
, min(qty) filter (where quarter = 'q4') as q4
from it0422x
group by xyear
order by xyear;
year | q1 | q2 | q3 | q4
------+-----+-----+-----+-----
2017 | 200 | 250 | 235 | 266
2018 | 195 | 220 | 215 | 240
(2 rows)
-- 使用補助表的方式
with t1 (quarter, q1, q2, q3, q4) as (
values ('q1', 1, 0, 0, 0)
, ('q2', 0, 1, 0, 0)
, ('q3', 0, 0, 1, 0)
, ('q4', 0, 0, 0, 1)
)
select x.xyear as year
, sum(x.qty * t1.q1) as q1
, sum(x.qty * t1.q2) as q2
, sum(x.qty * t1.q3) as q3
, sum(x.qty * t1.q4) as q4
from it0422x x
, t1
where x.quarter = t1.quarter
group by x.xyear
order by 1;
year | q1 | q2 | q3 | q4
------+-----+-----+-----+-----
2017 | 200 | 250 | 235 | 266
2018 | 195 | 220 | 215 | 240
(2 rows)
-- 使用 case when 方式
select xyear as year
, min(case
when quarter = 'q1' then qty
else null
end) as q1
, min(case
when quarter = 'q2' then qty
else null
end) as q2
, min(case
when quarter = 'q3' then qty
else null
end) as q3
, min(case
when quarter = 'q4' then qty
else null
end) as q4
from it0422x
group by xyear
order by xyear;
year | q1 | q2 | q3 | q4
------+-----+-----+-----+-----
2017 | 200 | 250 | 235 | 266
2018 | 195 | 220 | 215 | 240
(2 rows)
-- 大多數 RDBMS 都有 case when , filter 目前是 PostgreSQL 有
-- 可以觀察到, 使用 filter 語法較為簡潔
-- 不管是 補助表, case when , 還是 filter.其實三種方式的原理都是一樣的.
雖然樓上已經有許多解答了,但還是想分享自己練習的結果
--建表
DECLARE @XP AS TABLE
(
[年度] varchar(4) NOT NULL,
[Q1] decimal(11),
[Q2] decimal(11),
[Q3] decimal(11),
[Q4] decimal(11)
)
--放入參考資料
INSERT INTO @XP VALUES('2017',200,250,235,266)
INSERT INTO @XP VALUES('2018',195,220,215,240)
SELECT * FROM @XP
SELECT XP.[年度] 年度,a.各季,a.購買金額
FROM @XP XP
LEFT JOIN ( select [年度] 年度,'Q1' 各季,Q1 購買金額 from @XP ) a ON XP.年度 = a.年度
union
SELECT XP.[年度] 年度,a.各季,a.購買金額
FROM @XP XP
LEFT JOIN ( select [年度] 年度,'Q2' 各季,Q2 購買金額 from @XP ) a ON XP.年度 = a.年度
union
SELECT XP.[年度] 年度,a.各季,a.購買金額
FROM @XP XP
LEFT JOIN ( select [年度] 年度,'Q3' 各季,Q3 購買金額 from @XP ) a ON XP.年度 = a.年度
union
SELECT XP.[年度] 年度,a.各季,a.購買金額
FROM @XP XP
LEFT JOIN ( select [年度] 年度,'Q4' 各季,Q4 購買金額 from @XP ) a ON XP.年度 = a.年度