我有一个查询会检查表中的无效发票,然后检查同一个表 查看无效发票是否重新招标。我想知道的是......有没有办法 添加列以查看如果未重新投标无效发票,并在列中将其列为“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
我想要的一个例子:让我们说Invoice#123无效,从未重新招标......我想要 显示上面的内容并添加一个列标题“re-tendered”并列出“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
答案 0 :(得分: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))