我有下表,并尝试使用decode从列切换到行,但它要求group by子句。
PROD SITE L1 L2
---- ---- -- --
CSCO LA =< 5M , -.0001 =< 5M , .0001
IBM LA =< 5M , -.00005 =< 5M , .00005
IBM NY =< 1M , -.002 =< 1M , -.002
GOOG LA =< 2M , -.001 =< 2M , -.001
尝试切换到以下内容。你看到&#34; SITE&#34; name转到标题。
PROD LA-L1 LA-L2 NY-L1 NY-L2
---- ----- ----- ----- -----
CSCO =< 5M , -.0001 =< 5M , .0001
IBM =< 5M , -.00005 =< 5M , .00005 =< 1M , -.002 =< 1M , -.002
GOOG =< 2M , -.001 =< 2M , .001
这是查询:
SELECT * FROM (SELECT R.PROD,
DECODE(R.SITE,'LA',R.L1) "LA-L1",
DECODE(R.SITE,'LA',R.L2) "LA-L2",
DECODE(R.SITE,'NY',R.L1) "NY-L1",
DECODE(R.SITE,'NY',R.L2) "NY-L2"
FROM RESULTS R
GROUP BY R.PROD)
ORDER BY 1;
答案 0 :(得分:1)
SELECT * FROM (
SELECT R.PROD,
MAX(DECODE(R.SITE,'LA',R.L1)) "LA-L1",
MAX(DECODE(R.SITE,'LA',R.L2)) "LA-L2",
MAX(DECODE(R.SITE,'NY',R.L1)) "NY-L1",
MAX(DECODE(R.SITE,'NY',R.L2)) "NY-L2"
FROM RESULTS R
GROUP BY R.PROD)
ORDER BY 1;
当您通过PROD进行分组时,您需要一个聚合函数。我使用了MAX(它忽略了NULL)