2017-09-27 101 views
0

我有一個事務日期並需要從另一個表中返回一個字段。T-SQL如果日期大於或等於這個日期,則返回x如果不返回y

Table A Visit Bill  Transaction Date 
      1   1   12/20/2016 

我有另一個表我需要返回一個代碼,如果代碼日期是在交易日期之後。

Table B Bill CodeID Effective Code Date   Code 
      1  A  9/1/2015     ABCDE 
      1  B  9/1/2015     FGHI 
      1  C  9/1/2015     JKLM 
      1  A  10/01/2016    ZQMOP 
      1  B  10/01/2016    XYZNP 
      1  C  10/01/2016    EFGHI 

我該如何得到這個結果?

Vist Bill TransactionDate CodeID  Code 
1  1  12/20/2016  A  ZQMOP 
1  1  12/20/2016  B  XYZNP 
1  1  12/20/2016  C  EFGHI 
+0

你嘗試過這麼遠嗎?從你上面的例子來看,它們都不應該被返回,因爲它們都不是在交易日期之後。 – Eric

+0

@Eric對不起,我的意思是我需要返回最接近交易日期的生效日期的CodeID和代碼。例如:由於交易日期是2016年12月20日,我需要10/01/2016代碼。如果交易日期爲2015年9月2日,我需要返回生效日期爲2015年9月1日的代碼。我還沒有嘗試過任何東西。我沒有代碼。 – cmpmd2

+0

您的第一張桌子;它只包含一個值嗎?根據該表中的其他內容,對您的問題的各種答案可能會產生意外數量的行。 – Xedni

回答

0

這裏有一個方法,但也有其他浮現在腦海中(如使用row_number

前兩個熱膨脹係數是剛剛設置數據。然後查詢基本上將所有內容都拉到10/01/2016。該日期由選擇max(b.EffectiveCodeDate)的子查詢確定,該查詢嚴格小於該賬單的交易日期。

;with a as 
(
    select visit = 1, bill = 1, transactiondate = cast('12/20/2016' as date) 
), b (Bill, CodeId, EffectiveCodeDate, Code) as 
(
       select 1, 'A', cast('9/1/2015' as date),'ABCDE' 
    union all select 1, 'B', '9/1/2015','FGHI' 
    union all select 1, 'C', '9/1/2015','JKLM' 
    union all select 1, 'A', '10/01/2016','ZQMOP' 
    union all select 1, 'B', '10/01/2016','XYZNP' 
    union all select 1, 'C', '10/01/2016','EFGHI' 
) 
select * 
from b 
where b.EffectiveCodeDate = 
    (
     select max(b.EffectiveCodeDate) 
     from b 
     inner join a 
      on a.Bill = b.Bill 
      and b.EffectiveCodeDate < a.TransactionDate 
    ) 
0

我想下面的查詢工作

樣本數據:

create table #A (Visit int, Bill int, [Transaction Date] date) 
insert into #A values (1, 1, '12/20/2016') 

create table #B (Bill int, CodeID char(1), [Effective Code Date] date, Code varchar(10)) 
insert into #B values 
(1, 'A', '9/1/2015' , 'ABCDE'), 
(1, 'B', '9/1/2015' , 'FGHI'), 
(1, 'C', '9/1/2015' , 'JKLM'), 
(1, 'A', '10/01/2016', 'ZQMOP'), 
(1, 'B', '10/01/2016', 'XYZNP'), 
(1, 'C', '10/01/2016', 'EFGHI') 

查詢:

;with ct as (
    select * 
    , ROW_NUMBER() over (partition by Bill, CodeID order by [Effective Code Date] desc) RN 
    from #B 
) 
select #A.*, ct.CodeID, ct.Code 
from #A 
    inner join ct on ct.Bill = #A.Bill and ct.RN = 1 
0

我認爲這將解決它:

DECLARE @MinimumDiffrence int = (select Min(ABS(DATEDIFF(MILLISECOND,transaction_date,Effective_Code_Date))) 
           from A inner join B on A.Bill=B.Bill) 
select * 
from A 
inner join B 
on A.Bill = B.Bill 
where ABS(DATEDIFF(MILLISECOND,transaction_date,Effective_Code_Date)) = @MinimumDiffrence 

必須有一種方法來做到這一點只有一個查詢,我會努力找到它。

0

試試這個,

create table #A (Visit int, Bill int, [Transaction Date] date) 
insert into #A values (1, 1, '12/20/2016') 

create table #B (Bill int, CodeID char(1), 
[Effective Code Date] date, Code varchar(10)) 
insert into #B values 
(1, 'A', '9/1/2015' , 'ABCDE'), 
(1, 'B', '9/1/2015' , 'FGHI'), 
(1, 'C', '9/1/2015' , 'JKLM'), 
(1, 'A', '10/01/2016', 'ZQMOP'), 
(1, 'B', '10/01/2016', 'XYZNP'), 
(1, 'C', '10/01/2016', 'EFGHI') 

select * from 
#A a 
    inner join #B b on b.Bill = a.Bill 
where DATEDIFF(day,[Effective Code Date],[Transaction Date]) = 
(
select min(DATEDIFF(day,[Effective Code Date],[Transaction Date])) MinDiff 
from #A a 
    inner join #B b on b.Bill = a.Bill 

) 

    drop table #A 
    drop table #B 
0

我覺得這個方法,用CROSS APPLY最接近的問題描述要解決:

declare @ta table (Visit int not null, Bill int not null, TransactionDate date not null) 
insert into @ta (Visit,Bill,TransactionDate) values 
(1,1,'20161220') 

declare @tb table (Bill int not null, CodeID char(1) not null, 
        EffectiveCodeDate date not null, Code varchar(5) not null) 
insert into @tb (Bill,CodeID,EffectiveCodeDate,Code) values 
(1,'A','20150901','ABCDE'), 
(1,'B','20150901','FGHI'), 
(1,'C','20150901','JKLM'), 
(1,'A','20161001','ZQMOP'), 
(1,'B','20161001','XYZNP'), 
(1,'C','20161001','EFGHI') 

select 
    * 
from 
    @ta a 
     cross apply 
    (select *,ROW_NUMBER() OVER (PARTITION BY CodeID ORDER by EffectiveCodeDate desc) as rn 
    from @tb b 
    where 
     b.Bill = a.Bill and 
     b.EffectiveCodeDate < a.TransactionDate) p 
where 
    rn = 1 

也就是說,在每一行表B,並且對於每個不同的CodeID值,我們要選擇比表A早於TransactionDate的最新行。

結果:

Visit  Bill  TransactionDate Bill  CodeID EffectiveCodeDate Code rn 
----------- ----------- --------------- ----------- ------ ----------------- ----- -------------------- 
1   1   2016-12-20  1   A  2016-10-01  ZQMOP 1 
1   1   2016-12-20  1   B  2016-10-01  XYZNP 1 
1   1   2016-12-20  1   C  2016-10-01  EFGHI 1 

(作爲練習修剪結果集到你需要的列)

相關問題