我是StackOverflow的新手,所以我为格式错误道歉。
注意:我正在使用Oracle SQL Developer。
我正在尝试转换以下查询结果:
这样的事情:
目前我在主SQL中的工作原理,但它正在使用如下的子查询:
SELECT distinct(eprdgrp.name) product_group,
eprodpkg.name product_name,
(select count(creation_date) from ECPS_CA
where EXTRACT(month from CREATION_DATE)='2'
AND STATUS='STATUS_NAME'
AND ABC_CA.ID=ABC_CA.MASTER_ID
AND ABC_CA.PROD_PKG_ID IN (SELECT ID FROM ABC_PROD_PKG WHERE NAME=eprodpkg.name)) submission_feb,
(select count(creation_date) from ABC_CA
where EXTRACT(year from CREATION_DATE)=(2016)
and EXTRACT(month from CREATION_DATE)='3'
AND STATUS='STATUS_NAME'
AND ABC_CA.ID=ABC_CA.MASTER_ID
AND ABC_CA.PROD_PKG_ID IN (SELECT ID FROM ABC_PROD_PKG WHERE NAME=eprodpkg.name)) submission_mar,
...
我尝试使用此代码:
(select count(eca.creation_date) from dual
where EXTRACT(month from eca.CREATION_DATE)='2'
) submission_feb,
(select count(eca.creation_date) from dual
where EXTRACT(month from eca.CREATION_DATE)='3'
) submission_mar,
但它会产生不同的结果
似乎1和0只是指示是否存在product_group,product_name和month组合的结果。
感谢任何帮助,我希望我解释得很好!
提前致谢!
答案 0 :(得分:0)
试试这个
select
prod_name, name,
sum(Case when month(creation_date)=2 then 1 else 0 end) as submission_feb,
sum(Case when month(creation_date)=3 then 1 else 0 end) as submission_mar
from ECPS_CA
group by prod_name, name
答案 1 :(得分:0)
我会这样做:
select a.product_group, a.product_name, sum(a.submission_feb) as
submission_feb, sum(a.submission_mar) as submission_mar
from (
Select product_group, product_name, count(*) as submission_feb, 0 as
submission_mar
from ECPS_CA
where EXTRACT(month from CREATION_DATE)='2'
group by product_group, product_name
union all
Select product_group, product_name, 0, count(*)
from ECPS_CA
where EXTRACT(month from CREATION_DATE)='3'
group by product_group, product_name
) a
group by a.product_group, a.product_name
答案 2 :(得分:0)
您可以使用Oracle中的PIVOT函数:
SELECT *
FROM
(SELECT prod_group, prod_NAME, EXTRACT(MONTH FROM creation_date) AS creation_month
FROM ECPS_CA)
PIVOT (
COUNT(*) FOR creation_month IN (
1 AS SUBMISSION_JAN,
2 AS SUBMISSION_FEB,
3 AS SUBMISSION_MAR,
...
12 AS SUBMISSION_DEC
)
);