加入SQL Server表时显示空值

时间:2017-03-30 18:33:55

标签: sql sql-server

坚持这个作业并且它不应该显示空值。

说明:

  

列出每个部门处理的交易数量。包括
  没有处理任何交易的部门。显示结果   按部门升序排列。

这是我到目前为止所显示的事务,它显示了具有事务的部门的总事务,并跳过了空值。

SELECT 
    PC.Department, 
    COUNT(TD.TransactionID) AS TotalTransactions
FROM 
    TRANSACTION_DETAILS TD
INNER JOIN 
    PRODUCTS P ON P.ItemCode = TD.ItemCode
LEFT OUTER JOIN 
    PRODUCT_CATEGORIES PC ON P.Category = PC.Category
GROUP BY 
    PC.Department
ORDER BY 
    PC.Department ASC

2 个答案:

答案 0 :(得分:1)

SELECT      PC.Department, COUNT(TD.TransactionID) AS TotalTransactions
FROM        PRODUCT_CATEGORIES  PC 
LEFT JOIN   PRODUCTS            P   ON P.Category = PC.Category
LEFT JOIN   TRANSACTION_DETAILS TD  ON P.ItemCode = TD.ItemCode
GROUP BY    PC.Department
ORDER BY    PC.Department ASC

答案 1 :(得分:0)

如果您还需要没有价值的部门,您需要离开加入开始PRODUCT_CATEGORIES(部门)

  SELECT PC.Department, COUNT(TD.TransactionID) AS TotalTransactions
  FROM PRODUCT_CATEGORIES PC
  LEFT  JOIN PRODUCTS P ON P.Category = PC.Category
  LEFT  JOIN TRANSACTION_DETAILS TD ON P.ItemCode = TD.ItemCode
  GROUP BY PC.Department
  ORDER BY PC.Department ASC