Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have a dataset with the order number and another one with all items in those orders. Some of those items are a comment stating that the thing above is just a corrective invoice. I need to exclude the whole order containing comments like that. The problem is that the code I wrote, is only excluding the comment, but the item stays as it was, showing value. How to exclude the whole order if any of the items in this order are containing a specific keyword?

Example:

Order Number Database:

DocumnetNo
1111111

Items in this order

DocumnetNo | ItemName | LineTotalValue
1111111 | Shampoo 200ml | £9999
1111111 | Wrong invoice sent. | £0

What I have tried:

The code below excludes the item with the comment but the item without the keyword is causing the DocumentNo not to be entirely excluded.

My code so far:

SELECT
        'US' AS Region,
        T1.CustomerDocumentNo,
        T1.SOPOrderReturnID,
        T1.DocumentNo,
        T2.LineTotalValue,
        T2.LineQuantity,
        T2.SOPOrderReturnLineID,
        T1.DocumentDate,
        T1.RequestedDeliveryDate,
        T1.PromisedDeliveryDate,
        TotalNetValue AS OrderNetValue,
        TotalGrossValue AS OrderGrossValue,
        TotalTaxValue AS OrderTaxValue,
        T1.DocumentDueDate,
        T1.DateTimeCreated,
        T4.Name,
        T3.CustomerAccountName,
        T3.SLCustomerAccountID,
        T2.ItemCode,
        T2.ItemDescription,
        T5.StockItemTypeName,
        T6.SOPOrderReturnTypeName,
        T7.ItemName
    FROM
        SOPOrderReturn AS T1

        JOIN SOPOrderReturnLine AS T2 ON 
             T1.SOPOrderReturnID=T2.SOPOrderReturnID
        JOIN SLCustomerAccount AS T3 ON 
             T1.CustomerID=T3.SLCustomerAccountID
        JOIN DocumentStatus AS T4 ON 
             T1.DocumentStatusID=T4.DocumentStatusID
        JOIN StockItemType AS T5 ON 
             T2.StockItemTypeID=T5.StockItemTypeID
        JOIN SOPOrderReturnType AS T6 ON 
             T6.SOPOrderReturnTypeID=T1.DocumentTypeID
        LEFT JOIN SOPInvoiceCreditLine AS T7 ON 
             T7.SOPOrderReturnLineID=T2.SOPOrderReturnLineID

WHERE DocumentNo NOT IN (SELECT ItemName
					FROM SOPInvoiceCreditLine 
					WHERE ItemName LIKE'%Invoice%')
Posted
Updated 12-Jun-23 23:10pm
Comments
0x01AA 13-Jun-23 4:27am    
Try qualified name in where: WHERE T1.DocumentNo NOT IN ...

 
Share this answer
 
Thanks for your reply. I managed to find a solution myself (with a "little" help from ChatGPT)
and I'll post it here for the others.

So here goes:

WHERE T1.DocumentNo NOT IN (
      SELECT DISTINCT T8.DocumentNo
      FROM SOPOrderReturn AS T8

      JOIN SOPOrderReturnLine AS T9 ON 
            T8.SOPOrderReturnID = T9.SOPOrderReturnID
      JOIN SOPInvoiceCreditLine AS T10 ON 
            T10.SOPOrderReturnLineID = T9.SOPOrderReturnLineID

      WHERE T10.ItemName LIKE '%invoice%'
      OR T10.ItemName LIKE '%Credit%')
 
Share this answer
 
Identify the keyword column:
Determine the column in both tables that contains the keywords you want to exclude. Let's refer to this column as "KeywordColumn".

Create a new table or query:
Create a new table or query where you want to exclude the values based on keywords.

Use a WHERE clause with the NOT LIKE operator:
In your new table or query, use a WHERE clause with the NOT LIKE operator to exclude the values containing the keywords.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900