0

I have a query that will check a table for a voided invoice, and then check the same table to see if the voided invoice was re-tendered. What i'd like to know... is, is there a way to add a column to see If a voided invoice was NOT re-tendered, and list that as "N" in the column..

Here is my Current Query:

SELECT        t1.Store_Number, t1.Invoice_Number, t1.Invoice_Date, t1.Vehicle_Tag, t1.Void_Reason, t1.Void_Employee_Tax_Payer_Id, t1.Invoice_Total, t1.Void_Flag, 
                         t2.Invoice_Number AS Expr1, t2.Vehicle_Tag AS Expr2, t2.Invoice_Total AS Expr3
FROM            Invoice_Tb AS t1 INNER JOIN
                         Invoice_Tb AS t2 ON t1.Vehicle_Tag = t2.Vehicle_Tag
WHERE        (t1.Void_Flag = 'y') AND (t1.Invoice_Date BETWEEN CONVERT(DATETIME, '2012-08-01 00:00:00', 102) AND CONVERT(DATETIME, '2012-08-02 00:00:00', 102)) AND 
                         (t2.Void_Flag = 'n') AND (t2.Invoice_Date BETWEEN CONVERT(DATETIME, '2012-08-01 00:00:00', 102) AND CONVERT(DATETIME, '2012-08-02 00:00:00', 102))

Here is my current Output:

Store Number    Invoice Number  Date    Plate Number    Void Reason Employee    Invoice Amount Voided   New Invoice Number  Plate Number    New Invoice Amount  Re-Tendered
1568    3257714 8/1/2012 0:00   BBY1234 WRONG PRICE ENTERED 89556532    21.39   3257714 BBY1234 26.74   Y

An Example of what I would like: Lets say Invoice #123 was voided, and never re-tendered... I would want to show the above and add a column header "re-tendered" and list "n" where an invoice was not re-tendered. The output would be similar to this:

Store Number    Invoice Number  Date    Plate Number    Void Reason Employee    Invoice Amount Voided   New Invoice Number  Plate Number    New Invoice Amount  Re-Tendered
1568    3257714 8/1/2012 0:00   BBY1234 WRONG PRICE ENTERED 89556532    21.39   3257714 BBY1234 26.74   Y
1552    123 8/1/2012 0:00   Dwb0534 Wrong Plate number  73215654    15.95               N
Shmewnix
  • 1,553
  • 10
  • 32
  • 66

1 Answers1

1

you need a left join

SELECT        t1.Store_Number, t1.Invoice_Number, t1.Invoice_Date, t1.Vehicle_Tag, t1.Void_Reason, t1.Void_Employee_Tax_Payer_Id, t1.Invoice_Total, t1.Void_Flag, 
                         t2.Invoice_Number AS Expr1, t2.Vehicle_Tag AS Expr2, t2.Invoice_Total AS Expr3, case when t2.Vehicle_Tag is null then 'n' else 'y' end as retendered
FROM            Invoice_Tb AS t1 LEFT JOIN
                         Invoice_Tb AS t2 ON t1.Vehicle_Tag = t2.Vehicle_Tag And
                         (t2.Void_Flag = 'n') AND (t2.Invoice_Date BETWEEN CONVERT(DATETIME, '2012-08-01 00:00:00', 102) AND CONVERT(DATETIME, '2012-08-02 00:00:00', 102))
WHERE        (t1.Void_Flag = 'y') AND (t1.Invoice_Date BETWEEN CONVERT(DATETIME, '2012-08-01 00:00:00', 102) AND CONVERT(DATETIME, '2012-08-02 00:00:00', 102))  
iruvar
  • 22,736
  • 7
  • 53
  • 82