0

無効になった請求書のテーブルをチェックし、同じテーブルをチェックして、無効になった請求書が再提出されたかどうかを確認するクエリがあります。私が知りたいのは...無効になった請求書が再提出されていないかどうかを確認する列を追加し、列に「N」としてリストする方法はありますか..

ここに私の現在のクエリがあります:

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))

これが私の現在の出力です:

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

私が望むものの例: 請求書 #123 が無効にされ、再入札されなかったとしましょう... 上記を表示し、列ヘッダー「再入札」を追加し、請求書があった場所に「n」をリストします。再入札はありません。出力は次のようになります。

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
4

1 に答える 1

1

左結合が必要です

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))  
于 2012-08-05T16:54:34.990 に答える