2016-03-08 213 views
0

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

Desired result

+0

MySQL或SQL Server?他们不一样。 – squillman

+0

SQL Server对不起 –

+0

这是一个很好的开始。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –

回答

0

这就是我一直在寻找的,想通了。感谢Max对我如何解决这个问题提出了见解。

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.cost_centre, r.item, r.programme, r.activity, r.btl_sof, r.reverse_flag, --, ret1.returned_by, ret1.return_date 
     (case when r.reverse_flag = 'Y' then (select (un2.first_name + ' ' + un2.last_name)from Cashier..receipts as r1 
      inner join Cashier..user_name as un2 on un2.user_name=(UPPER(r1.created_by)) where created_date = (select MAX(created_date) 
           from Cashier..receipts where doc_no = r.doc_no)) end) as returned_by , 
     (case when r.reverse_flag = 'Y' then (select created_date from Cashier..receipts as r1 
      inner join Cashier..user_name as un2 on un2.user_name=(UPPER(r1.created_by)) where created_date = (select MAX(created_date) 
           from Cashier..receipts where doc_no = r.doc_no)) end) as returned_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)) 


where r.receipt_date = (select MIN(r1.receipt_date) from Cashier..receipts as r1 where r1.receipt_no = r.receipt_no) 
1

这是基本上你想要做什么?

-- test data creation, for me 
create table receipts (receipt_no int, receipt_date datetime, doc_no int, reverse_flag char(1), returned_by varchar(10), create_date datetime, created_date datetime) 
insert into receipts values (1, '1/1/2016', 12345, 'Y', 'John', null, '1/1/2016') 
insert into receipts values (2, '2/15/2016', 12345, 'N', null, '2/15/2016', '2/15/2016') 

SELECT r.receipt_date, r.receipt_no, r.doc_no, r.reverse_flag, ret1.return_date 
FROM receipts r 
INNER JOIN (
    SELECT doc_no, create_date as return_date 
    FROM receipts 
    WHERE reverse_flag = 'N')ret1 on r.doc_no = ret1.doc_no and ret1.return_date > r.receipt_date 
WHERE r.reverse_flag = 'Y' and r.doc_no = 12345 

如果这是你的目标,我想你刚才则将浏览到您的查询的末尾:

and r.receipt_date < ret1.return_date 

编辑:根据您的更新,我觉得则将浏览器端:

and convert(date, r.receipt_date) < convert(date, ret1.return_date) 
+0

输入内部连接时,如果输入了反向标志='N',则语句将变为false,结果为空。 –

+0

我刚刚编辑的第二个链接是期望的。 –

0

我还是不确定你想要什么。

select * 
from  (select doc_no, min(receipt_date) as min_receipt_date 
      from receipts group by doc_no) as min_receipt 
     left outer join (select doc_no, max(receipt_date) as max_receipt_date 
      from receipts group by doc_no) as max_receipt 
      on min_receipt.doc_no = max_receipt.doc_no and 
       min_receipt.min_receipt_date <> max_receipt.max_receipt_date 

insert into receipts values (1, '2016-01-01', 12345, 'Y', 'John', null, '2016-01-01'); 
insert into receipts values (2, '2016-03-15', 12345, 'N', null, '2016-03-15', '2016-03-15'); 

insert into receipts values (3, '2016-03-15', 123667, 'N', null, '2016-03-15', '2016-03-15'); 

产生

doc_no min_receipt_date   doc_no  max_receipt_date 
12345 January, 01 2016 00:00:00 12345  March, 15 2016 00:00:00 
123667 March, 15 2016 00:00:00  (null)  (null) 

但假设返回日期将永远是一样的原始收据日期。我也放弃了Y和N的标志,因为如果最短日期始终是原始购买日期,我不知道它是如何必要的。这里的其他答案使用created_date,但在表格数据的屏幕截图中,您只显示一个日期,因此我假设只有一个日期(接收日期)。我在MySQL上测试过,因为SQLFiddle讨厌插入我的SQL Server语法,我现在没有其他方法来测试。

+0

感谢您的帮助@gloomy。企鹅,我想我只是需要病例陈述,下次我会尝试尽可能清楚,我只是开始,仍然需要使用堆栈溢出。 –