SQL SELECT语句 - 根据第二个表中的匹配数选择记录

时间:2016-01-14 22:32:08

标签: sql

这是我的第一个问题,所以提前抱歉打破任何潜规则。我目前正在尝试使用InventoryCategory表中的主键从InventoryItem表中检索类别。

由于某个项目可以包含一个或多个类别,因此InventoryCategory表包含ItemCodeCategoryCodeIsPrimary(用于定义哪个类别是主要类别,并且TRUE / FALSE类型)

我确实需要对只有一个类别的所有项目使用主要类别,对具有两个类别的项目使用非主要类别。我正在尝试使用CASE语句将正确的值输出为命名的ParentCategory字段,但是时间很长。

目前,我只有WHERE IC.IsPrimary = 1,因为我无法弄清楚如何查询ItemCode以返回CategoryCodes的#并根据该选择非主要类别。我尝试做COUNT但它总是返回1并继续罢工。

代码:

SELECT 
    BillToCode, POSWorkstationID, 
    InvoiceDate, ItemName, 
    CategoryCode, 
    SUM(QtySales) QtySales, 
    SUM(Sales) Sales, 
    SUM(QtyReturns) QtyReturns, 
    SUM([Returns]) [Returns], 
    WarehouseCode   
FROM 
    (SELECT 
         CI.BillToCode, CI.WarehouseCode, 
         CI.POSWorkstationID, CI.InvoiceDate, 
         IC.CategoryCode, ii.ItemName, 
         CASE 
            WHEN CI.Type IN ('Invoice', 'Opening Invoice') 
              THEN CID.QuantityShipped 
              ELSE 0 
         END AS QtySales, 
         CASE 
            WHEN CI.Type IN ('Invoice', 'Opening Invoice') 
              THEN CID.ExtPriceRate 
              ELSE 0 
         END AS Sales, 
         CASE 
            WHEN CI.Type IN ('Credit Memo', 'Opening Credit', 'Gift Card', 'Gift Certificate') 
              THEN CID.QuantityShipped 
              ELSE 0 
         END AS QtyReturns,
         CASE 
            WHEN CI.Type IN ('Credit Memo', 'Opening Credit', 'Gift Card', 'Gift Certificate') 
              THEN CID.ExtPriceRate 
              ELSE 0 
         END AS [Returns]
     FROM 
         CustomerInvoice CI            
     INNER JOIN 
         Customer C ON CI.BillToCode = C.CustomerCode            
     INNER JOIN 
         CustomerInvoiceDetail CID ON CI.InvoiceCode = CID.InvoiceCode
     INNER JOIN 
         InventoryItem ii ON CID.ItemCode = ii.ItemCode    
     INNER JOIN 
         InventoryCategory IC ON CID.ItemCode = IC.ItemCode
     WHERE 
         (CI.IsBatch = 0 OR CI.IsBatch IS NULL) 
         AND CI.IsPosted = 1 
         AND CI.[Type] = 'Invoice' 
         AND CI.IsVoided = 0 
         AND IC.IsPrimary = 1) Items        
GROUP BY 
    POSWorkstationID, BillToCode, ItemName, InvoiceDate, CategoryCode, WarehouseCode

2 个答案:

答案 0 :(得分:1)

如果非主要行的IsPrimary字段为0,则可以执行SELECT MIN语句。见下文:

SELECT BillToCode        
, POSWorkstationID    
, InvoiceDate
, ItemName           
, CategoryCode
, SUM(QtySales) QtySales          
, SUM(Sales) Sales          
, SUM(QtyReturns) QtyReturns          
, SUM([Returns]) [Returns]       
, WarehouseCode   

FROM (          

SELECT CI.BillToCode
, CI.WarehouseCode
, CI.POSWorkstationID
, CI.InvoiceDate
, IC.CategoryCode
, ii.ItemName  
, CASE WHEN CI.Type IN ('Invoice', 'Opening Invoice') THEN CID.QuantityShipped ELSE 0 END AS QtySales            
, CASE WHEN CI.Type IN ('Invoice', 'Opening Invoice') THEN CID.ExtPriceRate ELSE 0 END AS Sales          
, CASE WHEN CI.Type IN ('Credit Memo', 'Opening Credit', 'Gift Card', 'Gift Certificate') THEN CID.QuantityShipped ELSE 0 END AS QtyReturns          
, CASE WHEN CI.Type IN ('Credit Memo', 'Opening Credit', 'Gift Card', 'Gift Certificate') THEN CID.ExtPriceRate ELSE 0 END AS [Returns]      

FROM CustomerInvoice CI            
INNER JOIN Customer C ON CI.BillToCode = C.CustomerCode            
INNER JOIN CustomerInvoiceDetail CID ON CI.InvoiceCode = CID.InvoiceCode
INNER JOIN InventoryItem ii ON CID.ItemCode = ii.ItemCode    
INNER JOIN InventoryCategory IC ON CID.ItemCode = IC.ItemCode
WHERE (CI.IsBatch = 0 OR CI.IsBatch IS NULL) 
AND CI.IsPosted = 1 
AND CI.[Type] = 'Invoice' AND CI.IsVoided = 0 
AND IC.IsPrimary = (SELECT MIN(IsPrimary) FROM InventoryCategory ICi WHERE ICi.ItemCode = IC.ItemCode)
) Items        

GROUP BY POSWorkstationID, BillToCode, ItemName, InvoiceDate, CategoryCode, WarehouseCode

答案 1 :(得分:0)

假设您的InventoryCategory数据如下所示:

py.test

使用“派生表”(子查询),而不是加入该表,而不是:

ItemCode CategoryCode IsPrimary
abc      def          1
abc      xyz          0
abc      123          0

然后在你的主select子句中你可以使用它(或者也许是一个case表达式):

inner join (
            select
                  ItemCode
                , max(case when IsPrimary = 1 then CategoryCode end) PrimCategory
                , max(case when IsPrimary = 0 then CategoryCode end) SecCategory
                , min(CategoryCode) as tertiaryCategory
            from InventoryCategory 
            group by ItemCode
           ) IC ON CID.ItemCode = IC.ItemCode