SQL PIvot返回值而不是计数

时间:2016-03-15 16:55:42

标签: sql oracle oracle-apex

我正在尝试创建一个SQL数据透视查询,其中返回结果是产品的名称而不是' 0'或者' 1'值(我正在寻找不同的值)。

这是我现有的查询:

SELECT *
FROM
( select
EBA_PROJ_STATUS.PROJECT AS PROJECT,
EBA_PROJ_STATUS.REGION AS REGION,
EBA_PROJ_STATUS_AIS.ONE_DB_PRODUCT_TYPE AS ONE_DB_PRODUCT_TYPE
from EBA_PROJ_STATUS EBA_PROJ_STATUS,
EBA_PROJ_STATUS_AIS EBA_PROJ_STATUS_AIS
where EBA_PROJ_STATUS_AIS.PROJECT_ID=EBA_PROJ_STATUS.ID
)
PIVOT (
COUNT (distinct ONE_DB_PRODUCT_TYPE) FOR (ONE_DB_PRODUCT_TYPE)
IN ('PROD1' as PROD1, 'PROD2' as PROD2, 'PROD3' as PROD3, 'PROD4' as PROD4, 'PROD4' as PROD5)
)

这会将每个产品作为PROD1,PROD2等标题,并在' 0'或者' 1'。有没有办法取0和1并将其作为产品字符串输出?

提前谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用值本身的聚合函数:

...
PIVOT (
MAX (ONE_DB_PRODUCT_TYPE) FOR (ONE_DB_PRODUCT_TYPE)
IN ('PROD1' as PROD1, 'PROD2' as PROD2, 'PROD3' as PROD3, 'PROD4' as PROD4, 'PROD4' as PROD5)
);

但是这样会得到如下结果:

PROJECT   REGI PROD1 PROD2 PROD3 PROD4 PROD5
--------- ---- ----- ----- ----- ----- -----
Project 1 EMEA       PROD2 PROD3            
Project 2 NA   PROD1       PROD3            

这似乎并不完全有用。如果要显示除空值以外的其他内容而不是现在的零,则可以NVL /合并外部选择中的值。