我有以下2个查询,我使用UNION ALL
加入了它们。对于count(distinct ID)
列和count(distinct src_id)
列,查询结果应位于不同的列中。对于count(distinct id)
,别名为IDl,对于count(distinct SRC_ID)
,别名为VOL
。此外,我的查询结果应如excel中提到的那样。我想要这种格式的结果,因为我想将查询结果粘贴到excel中,如果我在查询本身格式化它,它将减少我的手动工作。我之前直接尝试过使用PLSQL Procedure并在excel中获得结果,但它看起来非常复杂。我收到错误消息,因为我使用了UNION ALL
Select * from (
SELECT ID,src_id
FROM ckt_GRP
)
UNION ALL
select ID,src_id from TEST_GRP
)
pivot(count(distinct id) for tbl in('IDl' IDl))
pivot(count(distinct src_id) for tb2 in('VOL' VOL))
答案 0 :(得分:0)
我不会详细讨论您的查询,但请检查以下逻辑以创建单独的列,如果这是您想要的...
Select ID, Type, IDH, DMVOL from
( Select ID, Type, count(distinct HORDER.H_ORDER_BK) IDH, 0 DMVOL from Table_A
Union All
Select ID, Type, 0 IDH, count(distinct ordmast_srcid) DMVOL from Table_B )
P.S:我无法打开图片链接。
编辑:[试试这个]
Select sys_db_name,entity_id,ORD_DEL_DT,SUM(IDH) IDH, Sum(DM_VOL) DM_VOL from (
SELECT 'IDH' tb1,LDELIVERYORDER.sys_db_name sys_db_name,
SORDERMAIN.ENTITY_ID ENTITY_ID,
TO_NUMBER (TO_CHAR (ORD_DEL_DT, 'YYYYMMDD')) ORD_DEL_DT,
count(distinct HORDER.H_ORDER_BK) IDH,
0 DM_VOL
FROM INT_CDW_DV.L_DELIVERY_ORDER LDELIVERYORDER
INNER JOIN
INT_CDW_DV.H_ORDER HORDER
ON HORDER.H_ORDER_ID = LDELIVERYORDER.H_ORDER_ID
AND HORDER.SYS_DB_NAME = LDELIVERYORDER.SYS_DB_NAME
AND HORDER.SYS_DB_NAME in ('AS')
INNER JOIN
(SELECT S_ORDER_MAIN.ENTITY_ID,
S_ORDER_MAIN.H_ORDER_ID,
S_ORDER_MAIN.SYS_DB_NAME
FROM INT_CDW_DV.S_ORDER_MAIN
INNER JOIN
( SELECT S_ORDER_MAIN.H_ORDER_ID,
S_ORDER_MAIN.SYS_DB_NAME,
MAX (S_ORDER_MAIN.SYS_VALID_FROM_DTS)
AS SYS_VALID_FROM_DTS
FROM INT_CDW_DV.S_ORDER_MAIN
WHERE S_ORDER_MAIN.SYS_DB_NAME in ('AS')
GROUP BY S_ORDER_MAIN.H_ORDER_ID, S_ORDER_MAIN.SYS_DB_NAME) b
ON S_ORDER_MAIN.H_ORDER_ID = b.H_ORDER_ID
AND S_ORDER_MAIN.SYS_VALID_FROM_DTS =
b.SYS_VALID_FROM_DTS
AND S_ORDER_MAIN.SYS_DB_NAME = b.SYS_DB_NAME) SORDERMAIN
ON HORDER.H_ORDER_ID = SORDERMAIN.H_ORDER_ID
AND HORDER.SYS_DB_NAME = SORDERMAIN.SYS_DB_NAME
INNER JOIN
(SELECT S_DELIVERY_MAIN.ORD_DEL_DT,
S_DELIVERY_MAIN.H_DELIV_ID,
S_DELIVERY_MAIN.SYS_DB_NAME
FROM INT_CDW_DV.S_DELIVERY_MAIN
INNER JOIN
( SELECT S_DELIVERY_MAIN.H_DELIV_ID,
S_DELIVERY_MAIN.SYS_DB_NAME,
MAX (S_DELIVERY_MAIN.SYS_VALID_FROM_DTS)
AS SYS_VALID_FROM_DTS
FROM INT_CDW_DV.S_DELIVERY_MAIN
WHERE
S_DELIVERY_MAIN.SYS_DB_NAME in ('AS')
GROUP BY S_DELIVERY_MAIN.H_DELIV_ID,
S_DELIVERY_MAIN.SYS_DB_NAME) b
ON S_DELIVERY_MAIN.H_DELIV_ID = b.H_DELIV_ID
AND S_DELIVERY_MAIN.SYS_VALID_FROM_DTS =
b.SYS_VALID_FROM_DTS
AND S_DELIVERY_MAIN.SYS_DB_NAME = b.SYS_DB_NAME) SDELIVERYMAIN
ON LDELIVERYORDER.H_DELIV_ID = SDELIVERYMAIN.H_DELIV_ID
AND LDELIVERYORDER.SYS_DB_NAME = SDELIVERYMAIN.SYS_DB_NAME
WHERE ORD_DEL_DT between to_date ('20170717', 'YYYYMMDD') and to_date ('20170723', 'YYYYMMDD')
and LDELIVERYORDER.sys_db_name in ('AS') and SORDERMAIN.ENTITY_ID = 26
group by LDELIVERYORDER.sys_db_name,
SORDERMAIN.ENTITY_ID,
TO_NUMBER (TO_CHAR (ORD_DEL_DT, 'YYYYMMDD'))
order by LDELIVERYORDER.sys_db_name,
SORDERMAIN.ENTITY_ID,
TO_NUMBER (TO_CHAR (ORD_DEL_DT, 'YYYYMMDD'))
UNION ALL
select 'DMVOL' tb2,sys_db_name,entity_id,shipping_dayid ORD_DEL_DT, 0 IDH, count(distinct ordmast_srcid) DM_VOL
from int_pub_edw.dm_valid_order_line
where shipping_dayid between 20170717 and 20170723
and sys_db_name = 'AS'
and entity_id = 26
and valid_order=1
group by sys_db_name,entity_id,shipping_dayid
) group by sys_db_name,entity_id,ORD_DEL_DT