【问题标题】:Update multiple columns from one table with one column from another table用另一个表中的一列更新一个表中的多个列
【发布时间】:2015-04-02 23:09:45
【问题描述】:

我有三个按这种格式构造的表格:

DECLARE @A
(
    TypeName varchar(100),
    Type1 varchar(3),
    Type2 varchar(3),
    Type3 varchar(3),
    Type4 varchar(3),
    Type5 varchar(3),
    Type6 varchar(3)
)

Data:
| Bob   | null | null | null | null | null | null |
| Steve | null | null | null | null | null | null |
| Bill  | null | null | null | null | null | null |
...

DECLARE @B
(
    NameID int,
    Name varchar(100)
)

Data:
| 1 | Bob |
| 2 | Steve |
| 3 | Bill |
...

DECLARE @C
(
    NameID int,
    Type int
)

Data:
| 1 | 1 |
| 1 | 3 |
| 2 | 1 |
| 3 | 2 |
...

我想使用基于表 @CType 列的条件更新表 @A 中的 Type# 列。这是我尝试使用的 SQL 查询:

UPDATE @A SET
    Type1 = (CASE WHEN c.Type = 6 THEN 'Yes' ELSE 'No' END),
    Type2 = (CASE WHEN c.Type = 1 THEN 'Yes' ELSE 'No' END),
    Type3 = (CASE WHEN c.Type = 2 THEN 'Yes' ELSE 'No' END),
    Type4 = (CASE WHEN c.Type = 3 THEN 'Yes' ELSE 'No' END),
    Type5 = (CASE WHEN c.Type = 4 THEN 'Yes' ELSE 'No' END),
    Type6 = (CASE WHEN c.Type = 5 THEN 'Yes' ELSE 'No' END),
FROM @A
    INNER JOIN @B b ON b.Name = TypeName
    INNER JOIN @C c ON c.NameID = b.NameID

SELECT * FROM @A

而不是得到期望值:

| Bob   | No | Yes | No  | Yes | No | No | <-- Correct
| Steve | No | Yes | No  | No  | No | No |
| Bill  | No | No  | Yes | No  | No | No |

我得到了这些值:

| Bob   | No | Yes | No  | No  | No | No | <-- Incorrect (noticed the third column from the right being set to No instead of Yes)
| Steve | No | Yes | No  | No  | No | No |
| Bill  | No | No  | Yes | No  | No | No |

我也尝试过将语句重写为:

UPDATE @A SET
    Type1 = (CASE WHEN t.Type = 6 THEN 'Yes' ELSE 'No' END),
    Type2 = (CASE WHEN t.Type = 1 THEN 'Yes' ELSE 'No' END),
    Type3 = (CASE WHEN t.Type = 2 THEN 'Yes' ELSE 'No' END),
    Type4 = (CASE WHEN t.Type = 3 THEN 'Yes' ELSE 'No' END),
    Type5 = (CASE WHEN t.Type = 4 THEN 'Yes' ELSE 'No' END),
    Type6 = (CASE WHEN t.Type = 5 THEN 'Yes' ELSE 'No' END),
FROM
(
    SELECT b.Name, c.Type
    FROM @C c
        INNER JOIN @B b on b.NameID = c.NameID
) t
WHERE t.Name = TypeName

但还是返回了后面的结果。

那么,为什么 TypeNameBob 的列没有正确更新,我该如何解决这个问题,以便获得我的预期值?

【问题讨论】:

  • 表 A 中的 TypeAssigned6 是什么?
  • 应该是Type6 似乎编辑没有正确保存我的更改。我更新了它
  • 你确定你在加入所有 3 个表时只得到 3 行吗?你应该得到 4 行
  • @DevelopmentIsMyPassion 我确定我只得到 3 行,因为我只更新 @A 而不是插入。如果我在@C 上进行选择,那么我将得到 4 行。

标签: sql sql-server sql-server-2008 tsql sql-update


【解决方案1】:

问题是每个人在@C 中有几行。您必须先使其成为一行才能更新数据:

select
  NameID,
  max(case Type when 1 then 1 else 0 end) as Type1,
  max(case Type when 2 then 1 else 0 end) as Type2,
  max(case Type when 3 then 1 else 0 end) as Type3,
  max(case Type when 4 then 1 else 0 end) as Type4,
  max(case Type when 5 then 1 else 0 end) as Type5,
  max(case Type when 6 then 1 else 0 end) as Type6
from
  @C
group by
  NameID

现在无法对此进行测试,但您甚至可以在那里制作文本,如下所示:

case when max(case Type when 1 the 1 else 0) = 1 then 'Yes' else 'No' 以 Type1 结尾

您可以在更新中将其用作派生表,或者可以先将结果收集到另一个表变量中

【讨论】:

  • 这行得通!但我不明白max() 如何解决我的问题?如果我要删除 max(),然后用 Yes 和 No 替换 1 和 0,那么这将导致我当前的问题。
  • 当您在数据中每人有超过一行时,您将多次更新@A,它只是将最后的数据保留在那里。如果您执行与 select 相同的操作,您将弄清楚 - Bob 将有 2 行,在不同的列中带有 Yes。
猜你喜欢
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 2019-12-21
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
相关资源
最近更新 更多