2013-06-12 52 views
2

我有一個表像之下,它具有的SeqNo,代碼和日期T-SQL如何從另一個表和不確定行數的金額日期

data table1 

seqno  code date 
00009976 44 2010-05-04 00:00:00.000 
00021577 4 2010-11-02 15:10:54.193 
00021577 4 2011-03-16 16:58:35.880 
00021577 44 2011-09-29 00:00:00.000 
00003906 44 2012-06-25 00:00:00.000 
00029266 3 2011-01-07 08:42:15.407 
00029266 4 2011-08-08 15:47:33.337 
00029266 44 2011-10-24 00:00:00.000 
00012024 4 2011-03-01 09:28:09.790 
00012024 44 2012-01-09 00:00:00.000 
00006521 4 2011-12-28 08:55:23.567 
00006521 44 2012-05-17 00:00:00.000 
00071790 1 2011-09-02 09:23:33.000 
00071790 44 2012-04-23 00:00:00.000 
00008720 44 2012-04-03 00:00:00.000 
00010872 3 2012-06-25 13:49:50.197 
00010872 44 2012-09-11 00:00:00.000 

和像

data table2 
seqno  NUMBERS getdate 
00009976 504 2010-05-04 00:00:00.000 
00009976 53470 NULL 
00021577 10000 2010-12-17 00:00:00.000 
00021577 5000 2011-01-18 00:00:00.000 
00021577 2000 2011-03-16 00:00:00.000 
00021577 5000 2011-04-13 00:00:00.000 
00021577 3000 2011-04-13 00:00:00.000 
00021577 4000 2011-06-15 00:00:00.000 
00021577 2000 2011-05-19 00:00:00.000 
00021577 3000 2011-05-26 00:00:00.000 
00021577 5000 2011-05-26 00:00:00.000 
00021577 1000 2011-05-26 00:00:00.000 
00021577 5000 2011-05-26 00:00:00.000 
00021577 4000 2011-09-07 00:00:00.000 
00021577 11649 2011-09-29 00:00:00.000 
00003906 38665 NULL 
00029266 230 2011-05-06 00:00:00.000 
00029266 265 2011-05-11 00:00:00.000 
00029266 2400 2011-05-24 00:00:00.000 
00029266 11528 2011-09-22 00:00:00.000 
00029266 9379 2011-10-20 00:00:00.000 
00029266 12310 2011-10-24 00:00:00.000 
00012024 4124 2012-01-09 00:00:00.000 
00012024 5600 2012-01-09 00:00:00.000 
00012024 5600 2012-01-09 00:00:00.000 
00012024 5600 2012-01-09 00:00:00.000 
00012024 5600 2012-01-09 00:00:00.000 
00012024 5600 2012-01-09 00:00:00.000 
00012024 5600 2012-01-09 00:00:00.000 
00012024 4972 2012-01-09 00:00:00.000 
00006521 3611 2011-02-01 00:00:00.000 
00006521 8647 2011-02-01 00:00:00.000 
00006521 32413 2011-02-01 00:00:00.000 
00006521 137 2012-05-17 00:00:00.000 
00071790 50000 2011-10-28 00:00:00.000 
00071790 100000 2012-04-23 00:00:00.000 
00008720 61250 2012-04-03 00:00:00.000 
00010872 19773 2012-07-31 00:00:00.000 
00010872 46395 2012-09-11 00:00:00.000 
另一個表

現在我用從表2和更新正確的字段和表1和號碼的SeqNo和日期由代碼#resulttable,也許像

if code = '3' 
begin 
    update #resulttable 
    set code3 = a.num 
    from 
    (select sum(NUMBERS) num 
    from #table2 
    where seqno = "SEQNO" and getdate between "DATE1 from table1" and "DATE2 from table1" 
    ) a 
end 
else if code = '4' 
begin 
    update #resulttable 
    set code4 = a.num 
    from 
    (select sum(NUMBERS) num 
    from #table2 
    where seqno = "SEQNO" and getdate between "DATE1 from table1" and "DATE2 from table1" 
    ) a 
end 

我不知道如何獲得每個seqno的date1和date2到sql語句,如果相同的seqno有兩個以上的日期,我必須總結date2到date3和date3到date4也

現在我使用遊標和GOTO解決這個問題,但它真的很慢,我的代碼是像下面

declare @seqno char(8), @date1 datetime, @date2 datetime, @code1 char(2), @code2 char(2) 
DECLARE user_cursor CURSOR FORWARD_ONLY FAST_FORWARD FOR select distinct SEQNO from #table11 
OPEN user_cursor 
FETCH NEXT FROM user_cursor INTO @seqno 
while(@@FETCH_STATUS = 0) 
begin 
RERUN: 
    select top 1 @date1 = exec_date, @code1 = code from #table1 where seqno = @seqno order by getdate 
    delete #table1 where exec_year = seqno = @seqno and getdate = @date1 
    select top 1 @date2 = exec_date, @code2 = code from #table1 where seqno = @seqno order by getdate 
    if @code1 = '1' 
    begin 
     update #result_table 
     set code1 = a.all_pay + ISNULL(code1,0) 
     from #result_table t 
      inner join 
       (select seqno,sum(NUMBER) all_pay 
       from #table2 m 
       where (m_getdate between @date1 and @date2) 
       group by SEQNO 
       ) a on a.SEQNO = t.seqno 
     where t.seqno = @seqno 
    end 
    else if @code1 = '2' 
    begin 
     update #result_table 
     set code2 = a.all_pay + ISNULL(code2,0) 
     from #result_table t 
      inner join 
       (select seqno,sum(NUMBER) all_pay 
       from #table2 m 
       where (m_getdate between @date1 and @date2) 
       group by SEQNO 
       ) a on a.SEQNO = t.seqno  
     where t.seqno = @seqno 
    end 
if @code2 <> '44' 
    GOTO RERUN 

FETCH NEXT FROM user_cursor INTO @seqno 
end 
CLOSE user_cursor 
DEALLOCATE user_cursor 

可以將此代碼rerwite正常的T-SQL代碼,而無需光標?

結果應該是

seqno code1 code2 code3 code4 code5 code6 code7 code8 code9 
21577 NULL NULL NULL 60649 NULL NULL NULL NULL NULL 
29266 NULL NULL 2895 33217 NULL NULL NULL NULL NULL  
12024 NULL NULL NULL 42696 NULL NULL NULL NULL NULL  
6521 NULL NULL NULL 137 NULL NULL NULL NULL NULL  
71790 150000 NULL NULL NULL NULL NULL NULL NULL NULL  
8720 NULL NULL NULL NULL NULL NULL NULL NULL NULL  
10872 NULL NULL 66168 NULL NULL NULL NULL NULL NULL  

感謝@戈登 - linoff給我很好的建議,現在我的代碼是這樣

select dd.SEQNO, 
SUM(case when code = '1' then numbers end) as code1, 
SUM(case when code = '2' then numbers end) as code2, 
SUM(case when code = '3' then numbers end) as code3, 
SUM(case when code = '4' then numbers end) as code4, 
SUM(case when code = '5' then numbers end) as code5, 
SUM(case when code = '6' then numbers end) as code6, 
SUM(case when code = '7' then numbers end) as code7, 
SUM(case when code = '8' then numbers end) as code8, 
SUM(case when code = '9' then numbers end) as code9 
from #resulttable t 
join 
(select EXEC_YEAR,EXEC_CASE,EXEC_SEQNO, 
    (select top 1 t.code 
    from #table1 t 
    where t.seqno = m.SEQNO and m.getdate < t.date 
    ) as code, numbers 
from #table2 m 
) dd 
on dd.SEQNO = t.seqno 
group by dd.SEQNO 

但數字需要在透水日期代碼被放,是這可能嗎?

+0

您正在使用哪個版本的SQL Server? –

+0

這是SQL Server 2008 – Adaam

+0

根據您的示例數據發佈所需的輸出。考慮使用示例數據創建sqlfiddle以幫助那些可能幫助你的人。 – peterm

回答

0

您可以使用相關的子查詢從第一個表中的第二個表中的每一行獲取代碼。然後,您可以根據需要聚合並轉發它們:

select t.seqno, 
     sum(case when thecode = 'code1' then numbers end) as code1, 
     sum(case when thecode = 'code2' then numbers end) as code2, 
     . . . 
     sum(case when thecode = 'code9' then numbers end) as code9 
from (select rt.*, 
      (select top 1 code 
       from #anothertable at 
       where at.date <= rt.m_getdate and at.seqno = rt.seqno 
       order by at.date desc 
      ) as thecode 
     from #resulttable rt 
    ) t 
group by t.seqno 
+0

謝謝@gordon,但日期需要在date1和date2之間,如果使用at.date <= rt.m_getdate會添加太多的數字。 – Adaam

+0

,數據應該來自表1和表2,#resulttable一開始沒有任何內容。也許前面的編輯​​表名混淆你。我糾正它。 – Adaam

+0

感謝@ gordon-linoff的代碼真的很有幫助,但是這裏有個新問題,就是代碼字段把透明代碼放到了什麼地方? – Adaam

相關問題