根据另一个字段为空

时间:2018-05-03 16:28:42

标签: sql sql-server-2014

我在SQL Server 2014上有以下查询来获取申请行信息。我在“COUNT_OF_REQ_LINES”列上获得了一个Count,它正常工作,但我的要求是当另一个字段'REQ_ID'为空时,我希望列'COUNT_OF_REQ_LINES'中的计数为0(零)。或者,我只想对COUNT_OF_REQ_LINES 进行聚合(Count),如果 REQ_ID不为空,则当该字段为非聚合时,其值仍为0。

SQL:

SELECT B.PO_ID, COUNT(C.LINE_NBR) AS 'CNT LINE_NBR', 
F.VENDOR_ID, A.REQ_ID, COUNT(A.REQ_LINE_NBR) AS 
'COUNT_OF_REQ_LINES'

FROM PS_PO_HDR B, PS_PO_LINE C, (PS_PO_LINE_SHIP D LEFT OUTER JOIN  
PS_PO_LINE_DISTRIB A ON  D.BUSINESS_UNIT = A.BUSINESS_UNIT AND D.PO_ID = 
A.PO_ID AND D.LINE_NBR = A.LINE_NBR AND D.SCHED_NBR = A.SCHED_NBR ), 
PS_ITM_CAT_TBL E, PS_VENDOR F 

WHERE C.BUSINESS_UNIT = B.BUSINESS_UNIT 
 AND C.PO_ID = B.PO_ID 
 AND D.BUSINESS_UNIT = B.BUSINESS_UNIT 
 AND D.PO_ID = B.PO_ID 
 AND D.LINE_NBR = C.LINE_NBR 
 AND E.CATEGORY_ID = C.CATEGORY_ID 
 AND E.EFFDT = 
    (SELECT MAX(E_ED.EFFDT) FROM PS_ITM_CAT_TBL E_ED 
    WHERE E.SETID = E_ED.SETID 
      AND E.CATEGORY_TYPE = E_ED.CATEGORY_TYPE 
      AND E.CATEGORY_CD = E_ED.CATEGORY_CD 
      AND E.CATEGORY_ID = E_ED.CATEGORY_ID 
      AND E_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) 
 AND F.VENDOR_ID = B.VENDOR_ID 

GROUP BY   B.PO_ID,  F.VENDOR_ID, A.REQ_ID 

ORDER BY 1, 2

我正在考虑使用CASE语句,但不确定我是否可以根据另一个字段使用它来设置字段上的值。

上述SQL的示例结果:

DEPTID     PO_ID      CNT_LINE_NBR     VENDOR_ID     REQ_ID     COUNT_OF_REQ_LINES     
681        A9062      2                JUDE          03775      2          
832        A8061      1                OWENS         13774      1
410        B9816      1                UNPOW         52866      1
600        A9743      3                DELL          52237      3
602        A4132      1                MCKIN                    1
604        A2145      1                EASTM         45523      1

从上面的结果可以看出,PO_ID A4132的行对于REQ_ID有一个空值(非空),但COUNT_OF_REQ_LINES正在聚合为1(当我希望它在这种情况下为0时)

如果我运行与上面相同的SQL,除了没有在A.REQ_LINE_NBR上执行COUNT而不是相同的PO_ID结果为0.感谢任何建议!

修改

这是我更新的查询,其中包含CASE WHEN语句,但现在它不喜欢GROUP BY字段。我现在必须添加A.REQ_ID和A.REQ_LINE_NBR,这将运行查询但是它没有正确地聚合计数。

SELECT B.PO_ID, COUNT(C.LINE_NBR) AS 'CNT LINE_NBR', 
F.VENDOR_ID, A.REQ_ID, 
 CASE WHEN A.REQ_LINE_NBR = '' THEN 0 ELSE COUNT(A.REQ_LINE_NBR) END AS 
'COUNT_OF_REQ_LINES' 

FROM PS_PO_HDR B, PS_PO_LINE C, (PS_PO_LINE_SHIP D LEFT OUTER JOIN  
 PS_PO_LINE_DISTRIB A ON  D.BUSINESS_UNIT = A.BUSINESS_UNIT AND D.PO_ID = 
 A.PO_ID AND D.LINE_NBR = A.LINE_NBR AND D.SCHED_NBR = A.SCHED_NBR ), 
 PS_ITM_CAT_TBL E, PS_VENDOR F 
WHERE C.BUSINESS_UNIT = B.BUSINESS_UNIT 
 AND C.PO_ID = B.PO_ID 
 AND C.CANCEL_STATUS IN ('A','C') 
 AND D.BUSINESS_UNIT = B.BUSINESS_UNIT 
 AND D.PO_ID = B.PO_ID 
 AND D.LINE_NBR = C.LINE_NBR 
 AND E.CATEGORY_ID = C.CATEGORY_ID 
 AND E.EFFDT = 
    (SELECT MAX(E_ED.EFFDT) FROM PS_ITM_CAT_TBL E_ED 
    WHERE E.SETID = E_ED.SETID 
      AND E.CATEGORY_TYPE = E_ED.CATEGORY_TYPE 
      AND E.CATEGORY_CD = E_ED.CATEGORY_CD 
      AND E.CATEGORY_ID = E_ED.CATEGORY_ID 
      AND E_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) 
 AND F.VENDOR_ID = B.VENDOR_ID 

GROUP BY  B.PO_ID,  F.VENDOR_ID, A.REQ_ID
ORDER BY 1,2

1 个答案:

答案 0 :(得分:1)

试试这个。区别在于CASE检查req_id是否为&#39;&#39;。在您的查询中,您正在检查req_line_nbr是否为&#39;&#39;这会给你一个错误,因为它不在GROUP BY

SELECT 
    B.PO_ID
  , COUNT(C.LINE_NBR) AS 'CNT LINE_NBR'
  , F.VENDOR_ID
  , A.REQ_ID.
  , CASE WHEN req_id = '' THEN 0 ELSE COUNT(A.REQ_LINE_NBR) END AS 'COUNT_OF_REQ_LINES'
FROM PS_PO_HDR B, PS_PO_LINE C, (PS_PO_LINE_SHIP D LEFT OUTER JOIN  
PS_PO_LINE_DISTRIB A ON  D.BUSINESS_UNIT = A.BUSINESS_UNIT AND D.PO_ID = 
A.PO_ID AND D.LINE_NBR = A.LINE_NBR AND D.SCHED_NBR = A.SCHED_NBR ), 
PS_ITM_CAT_TBL E, PS_VENDOR F 
WHERE C.BUSINESS_UNIT = B.BUSINESS_UNIT 
 AND C.PO_ID = B.PO_ID 
 AND D.BUSINESS_UNIT = B.BUSINESS_UNIT 
 AND D.PO_ID = B.PO_ID 
 AND D.LINE_NBR = C.LINE_NBR 
 AND E.CATEGORY_ID = C.CATEGORY_ID 
 AND E.EFFDT = 
    (SELECT MAX(E_ED.EFFDT) FROM PS_ITM_CAT_TBL E_ED 
    WHERE E.SETID = E_ED.SETID 
      AND E.CATEGORY_TYPE = E_ED.CATEGORY_TYPE 
      AND E.CATEGORY_CD = E_ED.CATEGORY_CD 
      AND E.CATEGORY_ID = E_ED.CATEGORY_ID 
      AND E_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) 
 AND F.VENDOR_ID = B.VENDOR_ID 
GROUP BY   B.PO_ID,  F.VENDOR_ID, A.REQ_ID 
ORDER BY 1, 2