任何幫助表示讚賞顯示,多值IF語句,在選擇
下面的代碼是從有人做一個數據庫,每次收據是由獨特的收據ID發出。當發生逆轉時發出新收據。兩者之間的聯繫是多少。如果發出反轉,則舊收據上的反向標誌更改爲Y,而新標誌更改爲N.我的查詢中選擇了「最短日期」和「最大數據」,因爲返回的收據將有更晚的日期,當它最初創建時。問題是,當沒有反向時,它仍然會提取信息,因爲最小和最大日期是相同的。我完全知道我需要一個if語句,但不知道該怎麼做,因爲我是數據庫的新手。
Select distinct r.receipt_date, r.receipt_no, r.doc_no as Payin_No,r.trans_amt,l.location_desc, ct.charge_type_desc,
(select un.first_name + ' ' + un.last_name)as cashier,
r.payee, r.comments, r.reverse_flag, ret1.returned_by, ret1.return_date
from Cashier..receipts as r
inner join Cashier..location as l on r.location_id=l.location_id
inner join [Cashier].[dbo].[charge_type] as ct on ct.charge_type_no=r.charge_type_no
inner join Cashier..user_name as un on un.user_name=(UPPER(r.created_by))
inner join (
select receipt_no as Return_Receipt ,
(select un2.first_name + ' ' + un2.last_name) as returned_by,
created_date as return_date, doc_no as Payin_no,
r1.reverse_flag
from Cashier..receipts as r1
inner join Cashier..user_name as un2 on un2.user_name=(UPPER(r1.created_by))
where doc_no = r1.doc_no
and created_date = (select MAX(created_date)
from Cashier..receipts where doc_no = r1.doc_no)) as ret1
on (ret1.Payin_no=r.doc_no)
where r.receipt_date = (select MIN(r1.receipt_date) from Cashier..receipts as r1 where r1.receipt_no = r.receipt_no)
Issue i am having, the return by is the same as created
MySQL或SQL Server?他們不一樣。 – squillman
SQL Server對不起 –
這是一個很好的開始。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –