简体   繁体   English

Oracle SQL中单行中的常见多行

[英]Common multiple rows in single row in oracle sql

I have below query where instead of creating multiple entries(rows) for the same BN it should create entry in same row with FC,FM,MS value. 我在下面查询,而不是为同一BN创建多个条目(行),而应在具有FC,FM,MS值的同一行中创建条目。

select bspt.BN, FC, FM, MS from(select distinct(ozt.BRD_ID),
bp.NAME as BN, 
os.OSS FC, '' FM, '' MS
from PR_BP ozt, PR_BP_BPP bp, BDD b, OS_VIEW os 
where bp.BRD_ID = ozt.BRD_ID AND ozt.BRD_ID = os.OBJECT_ID and 
os.OST = 'PR_BP_BPP' AND (ozt.BID = 10001)
and ozt.BID = b.BID AND ozt.BP_ACTIVE = 'Y'
UNION ALL
select distinct(ozt.BRD_ID),
bp.NAME as BN, 
'' FC, os.OST FM, '' MS
from PR_BP ozt, PR_BP_BPP bp, BRAND b, OS_VIEW os 
where bp.BRD_ID = ozt.BRD_ID AND ozt.BRD_ID = os.OBJECT_ID and 
os.OST = 'PR_BP_BPP' AND (ozt.BID = 10002)
and ozt.BID = b.BID AND ozt.BP_ACTIVE = 'Y'

And the above query is generating output with multiple entries for BNAME column. 上面的查询正在为BNAME列生成带有多个条目的输出。 So instead of creating separate entry,it should create only single row for common BNAME. 因此,与其创建单独的条目,不如为通用BNAME只创建一行。

All of this can be done with one query with conditional aggregation. 所有这些都可以通过一个条件聚集查询来完成。

select bp.NAME as BIZNAME, 
max(case when bbp.BRAND_ID = 10001 then os.OBJECT_STATUS end) FC,
max(case when bbp.BRAND_ID = 10002 then os.OBJECT_STATUS end) FM, 
max(case when bbp.BRAND_ID = 10007 then os.OBJECT_STATUS end) MS 
from PR_BP bbp
JOIN PR_BP_BPP bp ON bp.BP_ID = bbp.BP_ID
JOIN BRAND b ON bbp.BRAND_ID = b.BRAND_ID
JOIN OS_VIEW os ON bbp.BP_ID = os.OBJECT_ID
where os.OBJECT_TYPE = 'PR_BP_BPP' AND bbp.BP_ACTIVE = 'Y'
group by bp.NAME

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM