在没有脚本重复的情况下在单个 select 语句中获取多个 count()

Getting the multiple count() in a sinle select statement without script repitiion

下面是一个脚本,用于在单个 select 中获取计数,其中包含大量代码冗余。我如何真正清理代码?

SELECT

  (SELECT COUNT(CHH.TRANSACTION_TYPE) FROM HW_PROD.ILCCHH CHH INNER JOIN HW_PROD.ILCOHD OHD ON CHH.CONTROL_NUMBER = OHD.CONTROL_NUMBER AND CHH.PICK_TICKET = OHD.TICKET_NUMBER AND CHH.CHHDC = OHD.OHDDC AND CHH.WHSE_IDENTITY = OHD.WHSE_IDENTITY WHERE CHH.TRANSACTION_TYPE='PR' AND ((LC_EXCLUDE_FLAG = 'Y' AND CHH.CHHWSE <> LC_EXCLUDE_WHSEPLANT) OR  ((LC_EXCLUDE_FLAG = '' AND CHH.CHHWSE = LC_EXCLUDE_WHSEPLANT))) AND CHH.CLIENT_IDENTITY= LN_CLIENT_IDENTITY AND CHH.WHSE_IDENTITY = LN_WHSE_IDENTITY AND CHH.ADD_DATE = CURRENT_DATE ) AS PR_COUNT_DAY_ONE,
  (SELECT COUNT(CHH.TRANSACTION_TYPE) FROM HW_PROD.ILCCHH CHH INNER JOIN HW_PROD.ILCOHD OHD ON CHH.CONTROL_NUMBER = OHD.CONTROL_NUMBER AND CHH.PICK_TICKET = OHD.TICKET_NUMBER AND CHH.CHHDC = OHD.OHDDC AND CHH.WHSE_IDENTITY = OHD.WHSE_IDENTITY WHERE CHH.TRANSACTION_TYPE='SR' AND ((LC_EXCLUDE_FLAG = 'Y' AND CHH.CHHWSE <> LC_EXCLUDE_WHSEPLANT) OR  ((LC_EXCLUDE_FLAG = '' AND CHH.CHHWSE = LC_EXCLUDE_WHSEPLANT))) AND CHH.CLIENT_IDENTITY= LN_CLIENT_IDENTITY AND CHH.WHSE_IDENTITY = LN_WHSE_IDENTITY AND CHH.ADD_DATE = CURRENT_DATE ) AS SR_COUNT_DAY_ONE,
  (SELECT COUNT(CHH.TRANSACTION_TYPE) FROM HW_PROD.ILCCHH CHH INNER JOIN HW_PROD.ILCOHD OHD ON CHH.CONTROL_NUMBER = OHD.CONTROL_NUMBER AND CHH.PICK_TICKET = OHD.TICKET_NUMBER AND CHH.CHHDC = OHD.OHDDC AND CHH.WHSE_IDENTITY = OHD.WHSE_IDENTITY WHERE CHH.TRANSACTION_TYPE='EX' AND ((LC_EXCLUDE_FLAG = 'Y' AND CHH.CHHWSE <> LC_EXCLUDE_WHSEPLANT) OR  ((LC_EXCLUDE_FLAG = '' AND CHH.CHHWSE = LC_EXCLUDE_WHSEPLANT))) AND CHH.CLIENT_IDENTITY= LN_CLIENT_IDENTITY AND CHH.WHSE_IDENTITY = LN_WHSE_IDENTITY AND CHH.ADD_DATE = CURRENT_DATE ) AS EX_COUNT_DAY_ONE,
  (SELECT COUNT(CHH.TRANSACTION_TYPE) FROM HW_PROD.ILCCHH CHH INNER JOIN HW_PROD.ILCOHD OHD ON CHH.CONTROL_NUMBER = OHD.CONTROL_NUMBER AND CHH.PICK_TICKET = OHD.TICKET_NUMBER AND CHH.CHHDC = OHD.OHDDC AND CHH.WHSE_IDENTITY = OHD.WHSE_IDENTITY WHERE CHH.TRANSACTION_TYPE='01' AND ((LC_EXCLUDE_FLAG = 'Y' AND CHH.CHHWSE <> LC_EXCLUDE_WHSEPLANT) OR  ((LC_EXCLUDE_FLAG = '' AND CHH.CHHWSE = LC_EXCLUDE_WHSEPLANT))) AND CHH.CLIENT_IDENTITY= LN_CLIENT_IDENTITY AND CHH.WHSE_IDENTITY = LN_WHSE_IDENTITY AND CHH.ADD_DATE = CURRENT_DATE ) AS RE_COUNT_DAY_ONE,
  (SELECT COUNT(CHH.TRANSACTION_TYPE) FROM HW_PROD.ILCCHH CHH INNER JOIN HW_PROD.ILCOHD OHD ON CHH.CONTROL_NUMBER = OHD.CONTROL_NUMBER AND CHH.PICK_TICKET = OHD.TICKET_NUMBER AND CHH.CHHDC = OHD.OHDDC AND CHH.WHSE_IDENTITY = OHD.WHSE_IDENTITY WHERE CHH.TRANSACTION_TYPE='33' AND ((LC_EXCLUDE_FLAG = 'Y' AND CHH.CHHWSE <> LC_EXCLUDE_WHSEPLANT) OR  ((LC_EXCLUDE_FLAG = '' AND CHH.CHHWSE = LC_EXCLUDE_WHSEPLANT))) AND CHH.CLIENT_IDENTITY= LN_CLIENT_IDENTITY AND CHH.WHSE_IDENTITY = LN_WHSE_IDENTITY AND CHH.ADD_DATE = CURRENT_DATE ) AS TR_COUNT_DAY_ONE,

  (SELECT COUNT(CHH.TRANSACTION_TYPE) FROM HW_PROD.ILCCHH CHH INNER JOIN HW_PROD.ILCOHD OHD ON CHH.CONTROL_NUMBER = OHD.CONTROL_NUMBER AND CHH.PICK_TICKET = OHD.TICKET_NUMBER AND CHH.CHHDC = OHD.OHDDC AND CHH.WHSE_IDENTITY = OHD.WHSE_IDENTITY WHERE CHH.TRANSACTION_TYPE='PR' AND ((LC_EXCLUDE_FLAG = 'Y' AND CHH.CHHWSE <> LC_EXCLUDE_WHSEPLANT) OR  ((LC_EXCLUDE_FLAG = '' AND CHH.CHHWSE = LC_EXCLUDE_WHSEPLANT))) AND CHH.CLIENT_IDENTITY= LN_CLIENT_IDENTITY AND CHH.WHSE_IDENTITY = LN_WHSE_IDENTITY AND CHH.ADD_DATE =  CURRENT_DATE - 1 DAYS) AS PR_COUNT_DAY_TWO,
  (SELECT COUNT(CHH.TRANSACTION_TYPE) FROM HW_PROD.ILCCHH CHH INNER JOIN HW_PROD.ILCOHD OHD ON CHH.CONTROL_NUMBER = OHD.CONTROL_NUMBER AND CHH.PICK_TICKET = OHD.TICKET_NUMBER AND CHH.CHHDC = OHD.OHDDC AND CHH.WHSE_IDENTITY = OHD.WHSE_IDENTITY WHERE CHH.TRANSACTION_TYPE='SR' AND ((LC_EXCLUDE_FLAG = 'Y' AND CHH.CHHWSE <> LC_EXCLUDE_WHSEPLANT) OR  ((LC_EXCLUDE_FLAG = '' AND CHH.CHHWSE = LC_EXCLUDE_WHSEPLANT))) AND CHH.CLIENT_IDENTITY= LN_CLIENT_IDENTITY AND CHH.WHSE_IDENTITY = LN_WHSE_IDENTITY AND CHH.ADD_DATE =  CURRENT_DATE - 1 DAYS) AS SR_COUNT_DAY_TWO,
  (SELECT COUNT(CHH.TRANSACTION_TYPE) FROM HW_PROD.ILCCHH CHH INNER JOIN HW_PROD.ILCOHD OHD ON CHH.CONTROL_NUMBER = OHD.CONTROL_NUMBER AND CHH.PICK_TICKET = OHD.TICKET_NUMBER AND CHH.CHHDC = OHD.OHDDC AND CHH.WHSE_IDENTITY = OHD.WHSE_IDENTITY WHERE CHH.TRANSACTION_TYPE='EX' AND ((LC_EXCLUDE_FLAG = 'Y' AND CHH.CHHWSE <> LC_EXCLUDE_WHSEPLANT) OR  ((LC_EXCLUDE_FLAG = '' AND CHH.CHHWSE = LC_EXCLUDE_WHSEPLANT))) AND CHH.CLIENT_IDENTITY= LN_CLIENT_IDENTITY AND CHH.WHSE_IDENTITY = LN_WHSE_IDENTITY AND CHH.ADD_DATE =  CURRENT_DATE - 1 DAYS) AS EX_COUNT_DAY_TWO,
  (SELECT COUNT(CHH.TRANSACTION_TYPE) FROM HW_PROD.ILCCHH CHH INNER JOIN HW_PROD.ILCOHD OHD ON CHH.CONTROL_NUMBER = OHD.CONTROL_NUMBER AND CHH.PICK_TICKET = OHD.TICKET_NUMBER AND CHH.CHHDC = OHD.OHDDC AND CHH.WHSE_IDENTITY = OHD.WHSE_IDENTITY WHERE CHH.TRANSACTION_TYPE='01' AND ((LC_EXCLUDE_FLAG = 'Y' AND CHH.CHHWSE <> LC_EXCLUDE_WHSEPLANT) OR  ((LC_EXCLUDE_FLAG = '' AND CHH.CHHWSE = LC_EXCLUDE_WHSEPLANT))) AND CHH.CLIENT_IDENTITY= LN_CLIENT_IDENTITY AND CHH.WHSE_IDENTITY = LN_WHSE_IDENTITY AND CHH.ADD_DATE =  CURRENT_DATE - 1 DAYS) AS RE_COUNT_DAY_TWO,
  (SELECT COUNT(CHH.TRANSACTION_TYPE) FROM HW_PROD.ILCCHH CHH INNER JOIN HW_PROD.ILCOHD OHD ON CHH.CONTROL_NUMBER = OHD.CONTROL_NUMBER AND CHH.PICK_TICKET = OHD.TICKET_NUMBER AND CHH.CHHDC = OHD.OHDDC AND CHH.WHSE_IDENTITY = OHD.WHSE_IDENTITY WHERE CHH.TRANSACTION_TYPE='33' AND ((LC_EXCLUDE_FLAG = 'Y' AND CHH.CHHWSE <> LC_EXCLUDE_WHSEPLANT) OR  ((LC_EXCLUDE_FLAG = '' AND CHH.CHHWSE = LC_EXCLUDE_WHSEPLANT))) AND CHH.CLIENT_IDENTITY= LN_CLIENT_IDENTITY AND CHH.WHSE_IDENTITY = LN_WHSE_IDENTITY AND CHH.ADD_DATE =  CURRENT_DATE - 1 DAYS) AS TR_COUNT_DAY_TWO

FROM MYSCHEMA.DUAL;

类似于:

select CHH.ADD_DATE, CHH.TRANSACTION_TYPE, count(1)
from HW_PROD.ILCCHH CHH 
JOIN HW_PROD.ILCOHD OHD 
    ON CHH.CONTROL_NUMBER = OHD.CONTROL_NUMBER 
   AND CHH.PICK_TICKET = OHD.TICKET_NUMBER 
   AND CHH.CHHDC = OHD.OHDDC 
   AND CHH.WHSE_IDENTITY = OHD.WHSE_IDENTITY
WHERE CHH.TRANSACTION_TYPE IN ('PR','SR','EX','01','33')
  AND CHH.ADD_DATE IN (CURRENT_DATE, CURRENT_DATE - 1 DAYS) 
  AND <remaining where clause>
GROUP BY CHH.ADD_DATE, CHH.TRANSACTION_TYPE

现在是转置这个的问题,但这最好在应用程序的另一层完成。

另一个解决方案是:

SELECT COUNT( CASE WHEN CHH.TRANSACTION_TYPE = 'PR' 
                   AND CHH.ADD_DATE = CURRENT_DATE
                  THEN 1
             END ) AS PR_COUNT_DAY_ONE
     , COUNT( CASE WHEN CHH.TRANSACTION_TYPE = 'SR' 
                   AND CHH.ADD_DATE = CURRENT_DATE
                  THEN 1
             END ) AS SR_COUNT_DAY_ONE
    , ...
FROM HW_PROD.ILCCHH CHH 
JOIN HW_PROD.ILCOHD OHD 
    ON CHH.CONTROL_NUMBER = OHD.CONTROL_NUMBER 
   ...
WHERE CHH.TRANSACTION_TYPE IN ('PR','SR','EX','01','33')
 AND CHH.ADD_DATE IN (CURRENT_DATE, CURRENT_DATE - 1 DAYS)
 AND ...