任何帮助表示赞赏显示,多值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/ –