2

我有一个如下表,它有一个 seqno、代码和日期

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

现在我使用 table1 中的 seqno 和日期以及 table2 中的总和数字,并通过代码将正确的字段更新为#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

这段代码可以重写为没有光标的普通 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    

感谢@gordon-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

但是数字需要放在上一个日期的代码中,这可能吗?

4

1 回答 1

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
于 2013-06-12T04:37:05.160 回答