SQL - 使用另一个表中的各个行更新行

时间:2018-02-13 21:42:04

标签: sql sql-server

尝试将数据插入到每个合适的行,其中包含来自Table1的匹配ID和DayNumber,称为#Test到Table2,称为#BlueTable

我正在使用此

CREATE TABLE #Test (
            ID int,
            DayNumber  nvarchar(1), 
            MNumber nvarchar(2),
            QLetter nvarchar(1),
            Comments    nvarchar(60)
        )

insert into #Test(ID,DayNumber,MNumber,QLetter,Comments) 
  values(55,'1','2','A','000000'),
    (55,'5','2','A','111111'),
    (66,'7','2','A','222222'),
    (66,'7','2','B','333333')

  CREATE TABLE #BlueTable (
            ID nvarchar(40),
            DayNumber  nvarchar(1),
            BL_MN   nvarchar(2),
            BL_QL   nvarchar(1),
            BL_CM   nvarchar(60),
            BL_MN2  nvarchar(2),
            BL_QL2  nvarchar(1),
            BL_CM2  nvarchar(60)
        )

 declare @i int=1

while (@i <=7)
begin
   insert into #BlueTable(ID,DayNumber) 
  values(55,@i),
     (66,@i)

   set @i=@i+1
end

declare @loop int=1,
    @loopWord nvarchar(MAX)=''

   while(@loop<=2)-- HOW UPDATE IS RAN
  begin
       exec('update #BlueTable set BL_MN'+@loopWord+'=#Test.MNumber,BL_QL'+@loopWord+'=#Test.QLetter,BL_CM'+@loopWord+'=#Test.Comments from #Test
 where #BlueTable.ID=#Test.ID and #BlueTable.DayNumber = #Test.DayNumber')

    set @loop =@loop+1
    set @loopWord=@loop
 end


select * from #Test
select * from #BlueTable order by ID
drop table #Test
drop table #BlueTable

我期待至少一些空值,但它似乎覆盖了以前的更新并填充了新数据。这就是我更新它的方式

我一直试图得到的输出在使用时应该看起来像这样(从#BlueTable中选择*,其中BL_CM不是按ID的空订单):我想要什么

ID  | DayNumber | BL_MN | BL_QL |    BL_CM     | BL_MN2 | BL_QL2 | BL_CM2 |
55       1          2       A       000000        null     null     null
55       5          2       A       111111        null     null     null
66       7          2       A       222222         2        B      333333

相反,我得到的东西看起来像这样:我得到的东西

 ID  | DayNumber | BL_MN | BL_QL |    BL_CM     | BL_MN2 | BL_QL2 | BL_CM2 |
 55      1          2        A        000000        2         A     000000
 55      5          2        A        111111        2         A     111111
 66      7          2        A        222222        2         A     222222

请帮助:(

3 个答案:

答案 0 :(得分:1)

看看这个查询。我建议你先得到你想要的结果。然后插入第二个表格。

insert into #BlueTable
select
    ID, DayNumber, max(case when rn = 1 then MNumber end)
    , max(case when rn = 1 then QLetter end)
    , max(case when rn = 1 then Comments end)
    , max(case when rn = 2 then MNumber end)
    , max(case when rn = 2 then QLetter end)
    , max(case when rn = 2 then Comments end)
from (
    select
        *, rn = row_number() over (partition by ID, DayNumber order by MNumber, QLetter)
    from 
        #Test
) t
group by ID, DayNumber

答案 1 :(得分:0)

简单的INNER JOIN更新应该适用于您的方案。以下是如何将列从表更新到另一个的示例。

UPDATE T1
SET T1.BL_CM2 = T2.BL_CM2, T1.BL_QL2 = T2.BL_QL2, ... more columns
FROM #Test T1
INNER JOIN #BlueTable T2
ON T1.ID = T2.ID AND T1.DAYNUMBER = T2.DAYNUMBER

修改

我认为您所看到的内容如下:

UPDATE T1
SET T1.BL_MN = T2.MNumber, T1.BL_QL = T2.QLetter, T1.BL_CM = T2.Comments, 
    T1.BL_MN2 = T3.MNumber, T1.BL_QL2 = T3.QLetter, T1.BL_CM2 = T3.Comments
FROM #BlueTable T1
LEFT JOIN #Test T2
ON T1.ID = T2.ID AND T1.DAYNUMBER = T2.DAYNUMBER AND T2.QLetter = 'A'
LEFT JOIN #Test T3
ON T1.ID = T3.ID AND T1.DAYNUMBER = T3.DAYNUMBER AND T3.QLetter = 'B'

答案 2 :(得分:0)

在我看来,当你插入记录时,你永远不会得到这个结果     进入#Test表,你重复了ID = 66,DayNumber = 7。

ID  | DayNumber | BL_MN | BL_QL |    BL_CM     | BL_MN2 | BL_QL2 | BL_CM2 |
55      1          2        A        000000        2         A     000000
55      5          2        A        111111        2         A     111111
66      7          2        A        222222        2         A     222222

所以,结果可能看起来像这样。如果你不想看到这个    结果,您可以仅通过 QLetter ='A'进行过滤。

ID  | DayNumber | BL_MN | BL_QL |    BL_CM     | BL_MN2 | BL_QL2 | BL_CM2 |
55      1          2        A        000000        2         A     000000
55      5          2        A        111111        2         A     111111
66      7          2        A        222222        2         A     222222
66      7          2        B        333333        2         B     333333

所以,我建议在填充数据后立即使用此查询    在做任何更新之前。对于此更新,您实际上不需要SQL过程。    只需将此SELECT查询转换为更新即可。希望,这会有所帮助。

    SELECT 
       B.ID, 
       B.DayNumber, 
       T.MNumber AS BL_MN_1, 
       T.QLetter AS BL_QL_1, 
       T.Comments AS BL_CM_1,
       T.MNumber AS BL_MN_2, 
       T.QLetter AS BL_QL_2, 
       T.Comments AS BL_CM_2
    FROM #BlueTable AS B
    INNER JOIN #Test T ON T.ID = B.ID AND T.DayNumber = B.DayNumber

    SELECT 
       B.ID, 
       B.DayNumber, 
       T.MNumber AS BL_MN_1, 
       T.QLetter AS BL_QL_1, 
       T.Comments AS BL_CM_1,
       T.MNumber AS BL_MN_2, 
       T.QLetter AS BL_QL_2, 
       T.Comments AS BL_CM_2
    FROM #BlueTable AS B
    INNER JOIN #Test T ON T.ID = B.ID AND T.DayNumber = B.DayNumber AND T.QLetter = 'A'

实际上,我想知道,你为什么要尝试更新#BlueTable    信息。对于这种情况,它会工作。如果你告诉我具体的    要求,也可以改变这个解决方案。祝你好运。