2016-03-08 202 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。企鵝,我想我只是需要病例陳述,下次我會嘗試儘可能清楚,我只是開始,仍然需要使用堆棧溢出。 –