列A和列B等于表2中列C的新值

时间:2016-07-22 14:08:59

标签: sql sql-server-2012

我被要求将一些SQL字段更新为更现代,更单一的术语。所以现在我正在将2列合并为一个新值并将该新值输入另一个表。我知道我希望老人现在平等,我只是不确定如何用更少的单词和正确的术语来知道如何寻求帮助。

最初设计此数据库的人使事情变得更加复杂。因此,让某些事情难以理解和理解......

尝试解释..目前我正在使用TABLE1中的数据并将其转换为将进入TABLE2的新值

UDATE:我最初错误地提出了我的表格

TABLE1                                     TABLE2
+---------+---------+---------+            +---------+---------+
|   ID    |   A     |    B    |            |    ID   |    C    |
+---------+---------+---------+            +---------+---------+
|   01    | DESCRPT1| STRIPED |            |    01   | ZEBRA   |
+---------+---------+---------+            +---------+---------+
|   01    | DESCRPT2|  HORSE  |            |    02   | SNAKE   |
+---------+---------+---------+            +---------+---------+
|   02    | DESCRPT1| SLIMEY  |            
+---------+---------+---------+                 
|   02    | DESCRPT2|  ROPE   |            
+---------+---------+---------+  

From TABLE1
If Value DESCRPT1 is 'Striped' and DESCIRP2 is 'Horse'
THEN insert 'Zebra' into TABLE2 column C where TABLE1.ID = TABLE2.ID
If Value DESCRPT1  is 'Slimey' and DESCIRP2 is 'Rope'
THEN insert 'Snake' into TABLE2 column C where TABLE1.ID = TABLE2.ID

注意:这是我在这里的第一篇文章,所以如果我遗漏任何信息或做错了。对不起:(

更新

tblEngagementAttributes                     tblEngagement
+---------+---------+---------+            +---------+---------+---------+
|   ID    |   A     |    B    |            |    ID   |  Client |    C    |
+---------+---------+---------+            +---------+---------+---------+
|   01    | DESCRPT1| STRIPED |            |    01   | John    | ZEBRA   |
+---------+---------+---------+            +---------+---------+---------+
|   01    | DESCRPT2|  HORSE  |            |    02   | Mark    | SNAKE   |
+---------+---------+---------+            +---------+---------+---------+
|   02    | DESCRPT1| SLIMEY  |            
+---------+---------+---------+                 
|   02    | DESCRPT2|  ROPE   |            
+---------+---------+---------+  

所以我发现的更多信息可能有助于解决这个问题。我正在将数据转换成的表已经是现有的表。这两个表具有共享外键“ID”

3 个答案:

答案 0 :(得分:2)

您可以使用INSERT INTO ... SELECT构造来实现您想要的效果。我在这里指出的技巧是在WHERE上使用TABLE1子句来限制只能以某种形式插入的记录。如果您省略了WHERE子句,查询仍会运行,但是您会为TABLE1中您不想映射到TABLE2的任何记录添加虚拟记录。

INSERT INTO TABLE2 (ID, C)
SELECT ID,
       CASE WHEN A = 'STRIPED' AND B = 'HORSE' THEN 'ZEBRA'
            WHEN A = 'SLIMEY'  AND B = 'ROPE'  THEN 'SNAKE'
            ELSE ''
       END
FROM TABLE1
WHERE A = 'STRIPED' AND B = 'HORSE' OR
      A = 'SLIMEY'  AND B = 'ROPE'

<强>更新

在您更改原始问题之前,我给出了上述答案。请尝试以下查询以获取更新的问题:

INSERT INTO TABLE2 (ID, C)
SELECT t.ID,
       CASE WHEN t.ZebraCol >= 2 THEN 'ZEBRA'
            WHEN t.SnakeCol >= 2 THEN 'SNAKE'
            ELSE ''
       END AS C
FROM
(
    SELECT ID,
           SUM(CASE WHEN (A = 'DESCRPT1' AND B = 'STRIPED') OR
                         (A = 'DESCRPT2' AND B = 'HORSE') THEN 1 ELSE 0 END) AS ZebraCol,
           SUM(CASE WHEN (A = 'DESCRPT1' AND B = 'SLIMEY') OR
                         (A = 'DESCRPT2' AND B = 'ROPE') THEN 1 ELSE 0 END) AS SnakeCol
    FROM TABLE1
    GROUP BY ID
) t
WHERE t.ZebraCol >= 2 OR t.SnakeCol >= 2

答案 1 :(得分:0)

您需要使用CASE声明,我之前从未使用INSERT声明,但这应该是一个很好的起点:

INSERT into table2(id, c)
    SELECT id,
           CASE WHEN table1.a = 'STRIPED' AND table1.b = 'HORSE' THEN 'ZEBRA'
           CASE WHEN table1.a = 'SLIMEY' AND table1.b = 'ROPE' THEN 'SNAKE'
           ELSE ''
           END
FROM table1
WHERE a = 'STRIPED' AND B = 'HORSE' OR
      a = 'SLIMEY' AND B = 'ROPE'

试一试。

蒂姆打败了我。

答案 2 :(得分:0)

我猜你会有比ZEBRASNAKE更多的动物,所以会提出一个更通用的解决方案。首先,您创建一个aditional表来存储描述符给每只动物的内容。这允许您添加或更改说明,而不会影响您的查询。

CREATE TABLE WHAT_IS
    (`W_ID` int, `D1` varchar(7), `D2` varchar(5), `IS_A` varchar(5))
;

INSERT INTO WHAT_IS
    (`W_ID`, `D1`, `D2`, `IS_A`)
VALUES
    (1, 'STRIPED', 'HORSE', 'ZEBRA'),
    (2, 'SLIMEY', 'ROPE', 'SNAKE')
;

SQL Fiddle DEMO:这假设每个ID只有一个'DESCRPT1'和一个'DESCRPT2'

INSERT INTO Table2 (`ID`, `C`)
SELECT T.`ID`, W.`IS_A` as `C`
FROM (
      SELECT `ID`, 
              MAX(CASE WHEN `A` = 'DESCRPT1' THEN `B` END) as D1,
              MAX(CASE WHEN `A` = 'DESCRPT2' THEN `B` END) as D2
      FROM Table1 t   
      GROUP BY `ID`
     ) T
JOIN WHAT_IS W
  ON T.`D1` = W.`D1`
 AND T.`D2` = W.`D2`

<强>输出

| ID |     C |
|----|-------|
|  1 | ZEBRA |
|  2 | SNAKE |