2017-05-05 32 views
0
SELECT  
    dbo.RtlStore.Description, 
    COUNT(dbo.InvProduct.U_OwnershipType) AS CONSIGNMENT, 
    COUNT(dbo.InvProduct.U_OwnershipType) AS OUTRIGHT, 
    COUNT(dbo.InvProduct.U_OwnershipType) AS HOURSEBRAND, 
    COUNT(dbo.InvProduct.U_OwnershipType) AS GOI, 
    COUNT(dbo.InvProduct.U_OwnershipType) AS OTHERS 
FROM 
    dbo.RtlStore 
LEFT OUTER JOIN 
    dbo.InvProduct 
LEFT OUTER JOIN 
    dbo.TrxTransactionSaleItem ON dbo.InvProduct.ProductKey = dbo.TrxTransactionSaleItem.ProductKey 
LEFT OUTER JOIN 
    dbo.TrxTransaction ON dbo.TrxTransactionSaleItem.TransactionKey = dbo.TrxTransaction.TransactionKey 
    ON dbo.RtlStore.StoreKey = dbo.TrxTransaction.StoreKey 
GROUP BY 
    dbo.RtlStore.Description 
HAVING  
    (COUNT(dbo.InvProduct.U_OwnershipType) = N'CONSIGNMENT') 
    AND (COUNT(dbo.InvProduct.U_OwnershipType) = N'OUTRIGHT') 
    AND (COUNT(dbo.InvProduct.U_OwnershipType) = N'HOUSEBRAND') 
    AND (COUNT(dbo.InvProduct.U_OwnershipType) = N'GOI') 
    AND (COUNT(dbo.InvProduct.U_OwnershipType) = N'OTHERS') 
+1

你在做什么是比较COUNT(dbo.InvProduct.U_OwnershipType),这是数字为“寄卖”,这是一个字符串,只能比较类似的数据类型.. – asmgx

+0

告诉我们什么你试图在这里做 – asmgx

+0

它是从一个组合框,我需要将它提取到列的报告。 PLS帮助 –

回答

0

您是否想要转发数据?如果是的话试试这个:

SELECT  
    dbo.RtlStore.Description, 
    max(case when dbo.InvProduct.U_OwnershipType = N'CONSIGNMENT' then 1 else null end) AS CONSIGNMENT, 
    max(case when dbo.InvProduct.U_OwnershipType = N'OUTRIGHT' then 1 else null end) AS OUTRIGHT, 
    max(case when dbo.InvProduct.U_OwnershipType = N'HOUSEBRAND' then 1 else null end) AS HOURSEBRAND, 
    max(case when dbo.InvProduct.U_OwnershipType = N'GOI' then 1 else null end) AS GOI, 
    max(case when dbo.InvProduct.U_OwnershipType = N'OTHERS' then 1 else null end) AS OTHERS 
FROM 
    dbo.RtlStore 
LEFT OUTER JOIN 
    dbo.InvProduct 
LEFT OUTER JOIN 
    dbo.TrxTransactionSaleItem ON dbo.InvProduct.ProductKey = dbo.TrxTransactionSaleItem.ProductKey 
LEFT OUTER JOIN 
    dbo.TrxTransaction ON dbo.TrxTransactionSaleItem.TransactionKey = dbo.TrxTransaction.TransactionKey 
    ON dbo.RtlStore.StoreKey = dbo.TrxTransaction.StoreKey 
GROUP BY 
    dbo.RtlStore.Description 
+0

WOAHHHHHHH。它的工作非常感谢你先生 –

0

尝试此刻评论的HAVING部分,看看是否会帮助,一旦你得到的结果,你可以使用具有

SELECT  dbo.RtlStore.Description, COUNT(dbo.InvProduct.U_OwnershipType) AS CONSIGNMENT, COUNT(dbo.InvProduct.U_OwnershipType) AS OUTRIGHT, COUNT(dbo.InvProduct.U_OwnershipType) 
         AS HOURSEBRAND, COUNT(dbo.InvProduct.U_OwnershipType) AS GOI, COUNT(dbo.InvProduct.U_OwnershipType) AS OTHERS 
FROM   dbo.RtlStore LEFT OUTER JOIN 
         dbo.InvProduct LEFT OUTER JOIN 
         dbo.TrxTransactionSaleItem ON dbo.InvProduct.ProductKey = dbo.TrxTransactionSaleItem.ProductKey LEFT OUTER JOIN 
         dbo.TrxTransaction ON dbo.TrxTransactionSaleItem.TransactionKey = dbo.TrxTransaction.TransactionKey ON dbo.RtlStore.StoreKey = dbo.TrxTransaction.StoreKey 
GROUP BY dbo.RtlStore.Description 
--HAVING  


--    (COUNT(dbo.InvProduct.U_OwnershipType) = N'CONSIGNMENT') 
--  AND (COUNT(dbo.InvProduct.U_OwnershipType) = N'OUTRIGHT') 
--  AND (COUNT(dbo.InvProduct.U_OwnershipType) = N'HOUSEBRAND') 
--  AND (COUNT(dbo.InvProduct.U_OwnershipType) = N'GOI') 
--  AND (COUNT(dbo.InvProduct.U_OwnershipType) = N'OTHERS') 
然后将其过滤
相关问题