请转换SQL查询结果时遇到问题。 我的SQL查询是:
SELECT atop.name AS top, aseco.name AS abei, aseco.abeiId AS CI, aseco.location AS pac, axrt.frk AS FRK
FROM atop INNER JOIN (aseco INNER JOIN axrt ON (aseco.abeiId = axrt.abeiId) AND (aseco.BCFId = axrt.BCFId) AND (aseco.topId = axrt.topId)) ON atop.topId = aseco.topId
WHERE (((axrt.ctp)<>8) AND ((aseco.abeiish)=5)) OR (((axrt.ctp)<>3) AND ((aseco.abeiish)=2) AND ((aseco.hpnmd)=2));
结果如下:
----------------------------------------------------------------
top | abei | CI | pac | FRK |
----------------------------------------------------------------
A | b | e | 1 | 12 |
A | b | e | 1 | 13 |
A | b | e | 1 | 14 |
A | c | t | 2 | 45 |
A | c | t | 2 | 56 |
A | c | t | 2 | 23 |
A | c | t | 2 | 29 |
A | c | t | 2 | 50 |
b | c | t | 1 | 11 |
b | c | t | 1 | 56 |
b | c | t | 1 | 78 |
----------------------------------------------------------------
我想转置这个结果,我想要这样:
----------------------------------------------------------------------------------
top | abei | CI | pac | FRK | frk1 | frk2 | frk3 | frk4 |...
---------------------------------------------------------------- -----------------
A | b | e | 1 | 12 | 13 | 14 | | |...
A | c | t | 2 | 45 | 56 | 23 | 29 | 50 |...
b | c | t | 1 | 11 | 56 | 56 | 78 | |...
----------------------------------------------------------------------------------
我尝试了这段代码但不起作用!
SELECT atop.name AS top, aseco.name AS abei, aseco.abeiId AS CI, aseco.location AS pac, axrt.frk AS FRK,frk1,frk2,frk3,frk4,frk5,frk6,frk7,frk8,frk9,frk10,frk11,frk12,
max(case when row=1 then data end) frk1,
max(case when row=2 then data end) frk2,
max(case when row=3 then data end) frk3,
max(case when row=4 then data end) frk4,
max(case when row=5 then data end) frk5,
max(case when row=6 then data end) frk6,
max(case when row=7 then data end) frk7,
max(case when row=8 then data end) frk8,
max(case when row=9 then data end) frk9,
max(case when row=10 then data end) frk10,
max(case when row=11 then data end) frk11,
max(case when row=12 then data end) frk12
FROM atop INNER JOIN (aseco INNER JOIN axrt ON (aseco.abeiId = axrt.abeiId) AND (aseco.BCFId = axrt.BCFId) AND (aseco.topId = axrt.topId)) ON atop.topId = aseco.topId
WHERE (((axrt.ctp)<>8) AND ((aseco.abeiish)=5)) OR (((axrt.ctp)<>3) AND ((aseco.abeiish)=2) AND ((aseco.hpnmd)=2));
感谢您的帮助。
答案 0 :(得分:1)
您需要group by
,并且您不需要select
中的额外列。试试这个:
SELECT atop.name AS top, aseco.name AS abei, aseco.abeiId AS CI, aseco.location AS pac, axrt.frk AS FRK,
max(case when row=1 then data end) frk1,
max(case when row=2 then data end) frk2,
max(case when row=3 then data end) frk3,
max(case when row=4 then data end) frk4,
max(case when row=5 then data end) frk5,
max(case when row=6 then data end) frk6,
max(case when row=7 then data end) frk7,
max(case when row=8 then data end) frk8,
max(case when row=9 then data end) frk9,
max(case when row=10 then data end) frk10,
max(case when row=11 then data end) frk11,
max(case when row=12 then data end) frk12
FROM atop INNER JOIN
(aseco INNER JOIN axrt ON (aseco.abeiId = axrt.abeiId) AND (aseco.BCFId = axrt.BCFId) AND (aseco.topId = axrt.topId)) ON atop.topId = aseco.topId
WHERE (((axrt.ctp)<>8) AND ((aseco.abeiish)=5)) OR (((axrt.ctp)<>3) AND ((aseco.abeiish)=2) AND ((aseco.hpnmd)=2))
GROUP BY atop.name, aseco.name, aseco.abeiId, aseco.location ;