我想找到一种方法来退回所有"发票项目"在列中,如果其中任何一个包含"保修信用"作为"发票项目描述"。我在下面发布了一张图片,试图让它变得更有意义。正如您在下面看到的那样,第二张发票即使显示了"保修信用"未列出。非常感谢任何帮助。
WHERE EXISTS
(SELECT ARII.Description
FROM
AR_Customer ARC
Inner JOIN AR_Customer_Site ARCS On ARC.Customer_Id = ARCS.Customer_Id AND ARCS.Customer_Id = ARC.Customer_Id
INNER JOIN AR_Customer_System ARCSYS On ARCS.Customer_Site_Id = ARCSYS.Customer_Site_Id
INNER JOIN AR_Branch ARB ON ARB.Branch_Id = ARC.Branch_Id
INNER JOIN AR_Invoice ARIN ON ARIN.Customer_Site_Id = ARCS.Customer_Site_Id
INNER JOIN AR_Invoice_Item ARII ON ARII.Invoice_Id = ARIN.Invoice_Id
WHERE ARII.[Description] LIKE ('Warranty Credit') OR ARII.[Description] = ('Warranty Credit T'))
AND ARIN.Invoice_Date > dateadd(MONTH, -1, getdate())
ORDER BY ARIN.Invoice_Number
答案 0 :(得分:1)
我认为你混淆了存在的回报。在这个用例中使用IN:
SELECT DISTINCT
ARC.Customer_Number As 'Customer Number',
ARC.Customer_Name AS 'Customer Name',
ARB.Branch_Code AS 'Site Branch',
ARCS.Site_Number As 'Site Number',
ARCS.Business_Name AS 'Site Name',
ARII.Quantity AS 'Invoice Item Quantity',
ARII.Rate AS 'Invoice Item Rate',
ARII.Amount AS 'Invoice Item Amount',
ARII.Description AS 'Invoice Item Description',
SYSY.System_Code AS 'System Code',
ARIN.Invoice_Number AS 'Invoice Number',
CONVERT(VARCHAR(10), ARIN.Invoice_Date, 10) AS 'Invoice Date',
ARCAT.Category_Code AS 'Category',
ARIN.Memo AS 'Resolution Notes'
FROM
AR_Customer ARC
Inner JOIN AR_Customer_Site ARCS On ARC.Customer_Id = ARCS.Customer_Id AND ARCS.Customer_Id = ARC.Customer_Id
INNER JOIN AR_Customer_System ARCSYS On ARCS.Customer_Site_Id = ARCSYS.Customer_Site_Id
INNER JOIN AR_Branch ARB ON ARB.Branch_Id = ARC.Branch_Id
INNER JOIN AR_Invoice ARIN ON ARIN.Customer_Site_Id = ARCS.Customer_Site_Id
INNER JOIN AR_Invoice_Item ARII ON ARII.Invoice_Id = ARIN.Invoice_Id
Inner JOIN SY_System SYSY On ARCSYS.System_Id = SYSY.System_Id
INNER JOIN SV_Service_Ticket SVVT ON ARCSYS.Customer_System_Id = SVVT.Customer_System_Id AND ARII.Service_Ticket_Id = SVVT.Service_Ticket_Id
INNER JOIN AR_Customer_Recurring ARCR On ARCSYS.Customer_System_Id = ARCR.Customer_System_Id
INNER JOIN AR_Item ARI On ARCR.Item_Id = ARI.Item_Id
Inner JOIN SS_Item_Type SSIT On ARI.Item_Type_Id = SSIT.Item_Type_Id
INNER JOIN AR_Category ARCAT ON ARCAT.Category_Id = ARI.Category_Id
WHERE ARIN.Invoice_Number in
(SELECT ARIN.Invoice_Number
FROM
AR_Customer ARC
Inner JOIN AR_Customer_Site ARCS On ARC.Customer_Id = ARCS.Customer_Id AND ARCS.Customer_Id = ARC.Customer_Id
INNER JOIN AR_Customer_System ARCSYS On ARCS.Customer_Site_Id = ARCSYS.Customer_Site_Id
INNER JOIN AR_Branch ARB ON ARB.Branch_Id = ARC.Branch_Id
INNER JOIN AR_Invoice ARIN ON ARIN.Customer_Site_Id = ARCS.Customer_Site_Id
INNER JOIN AR_Invoice_Item ARII ON ARII.Invoice_Id = ARIN.Invoice_Id
WHERE ARII.[Description] like ('%Warranty Credit%') OR ARII.[Description] = ('Warranty Credit T'))
AND ARIN.Invoice_Date > dateadd(MONTH, -1, getdate())
ORDER BY ARIN.Invoice_Number
或者,如果您将该子句与外部查询中的谓词相关联,则可能存在...即添加where子句以检查外部和内部查询。
SELECT DISTINCT
ARC.Customer_Number As 'Customer Number',
ARC.Customer_Name AS 'Customer Name',
ARB.Branch_Code AS 'Site Branch',
ARCS.Site_Number As 'Site Number',
ARCS.Business_Name AS 'Site Name',
ARII.Quantity AS 'Invoice Item Quantity',
ARII.Rate AS 'Invoice Item Rate',
ARII.Amount AS 'Invoice Item Amount',
ARII.Description AS 'Invoice Item Description',
SYSY.System_Code AS 'System Code',
ARIN.Invoice_Number AS 'Invoice Number',
CONVERT(VARCHAR(10), ARIN.Invoice_Date, 10) AS 'Invoice Date',
ARCAT.Category_Code AS 'Category',
ARIN.Memo AS 'Resolution Notes'
FROM
AR_Customer ARC
Inner JOIN AR_Customer_Site ARCS On ARC.Customer_Id = ARCS.Customer_Id AND ARCS.Customer_Id = ARC.Customer_Id
INNER JOIN AR_Customer_System ARCSYS On ARCS.Customer_Site_Id = ARCSYS.Customer_Site_Id
INNER JOIN AR_Branch ARB ON ARB.Branch_Id = ARC.Branch_Id
INNER JOIN AR_Invoice ARIN ON ARIN.Customer_Site_Id = ARCS.Customer_Site_Id
INNER JOIN AR_Invoice_Item ARII ON ARII.Invoice_Id = ARIN.Invoice_Id
Inner JOIN SY_System SYSY On ARCSYS.System_Id = SYSY.System_Id
INNER JOIN SV_Service_Ticket SVVT ON ARCSYS.Customer_System_Id = SVVT.Customer_System_Id AND ARII.Service_Ticket_Id = SVVT.Service_Ticket_Id
INNER JOIN AR_Customer_Recurring ARCR On ARCSYS.Customer_System_Id = ARCR.Customer_System_Id
INNER JOIN AR_Item ARI On ARCR.Item_Id = ARI.Item_Id
Inner JOIN SS_Item_Type SSIT On ARI.Item_Type_Id = SSIT.Item_Type_Id
INNER JOIN AR_Category ARCAT ON ARCAT.Category_Id = ARI.Category_Id
WHERE ARIN.Invoice_Number in
(SELECT ARIN2.Invoice_Number
FROM
AR_Customer ARC
Inner JOIN AR_Customer_Site ARCS On ARC.Customer_Id = ARCS.Customer_Id AND ARCS.Customer_Id = ARC.Customer_Id
INNER JOIN AR_Customer_System ARCSYS On ARCS.Customer_Site_Id = ARCSYS.Customer_Site_Id
INNER JOIN AR_Branch ARB ON ARB.Branch_Id = ARC.Branch_Id
INNER JOIN AR_Invoice ARIN2 ON ARIN2.Customer_Site_Id = ARCS.Customer_Site_Id
INNER JOIN AR_Invoice_Item ARII ON ARII.Invoice_Id = ARIN.Invoice_Id
WHERE
(ARII.[Description] like ('%Warranty Credit%') OR ARII.[Description] = ('Warranty Credit T'))
and ARIN2.Invoice_Number = ARIN.Invoice_Number) --notice this part
AND ARIN.Invoice_Date > dateadd(MONTH, -1, getdate())
ORDER BY ARIN.Invoice_Number