如何根据特定约束条件将一个SQL表格和列中的数据导入到同一列中?

huangapple go评论67阅读模式
英文:

How do I bring data from one SQL table and column into the same column depedent on specific contraints?

问题

例如,我想要传输的数据位于Table1中的example_Date列中。所涉及的应用程序使用此列来存储多个数据字段ID的数据。

Table1:

表ID  字段ID  对象ID  对象Inv  example_Date
--------------------------------------------------
1         2         1       3        01/01/1996
1         4         1       3        01/02/1996
1         6         1       3        01/01/1996
1         2         2       4        01/05/1996
1         4         2       4        01/01/1997
1         6         2       4        10/01/1998

然后假设我想要获取具有字段ID为2的example_date中的数据,并将其复制到具有以下约束的同一表中的任何内容:

  1. 对象ID = 对象ID
  2. 对象Inv = 对象Inv
  3. 字段ID = 6

结果将如下所示(*表示已更新的数据)

Table1:

表ID  字段ID  对象ID  对象Inv  example_Date
-------------------------------------------------
1         2         1       3        01/01/1996
1         4         1       3        01/02/1996
1         6         1       3       *01/01/1996
1         2         2       4        01/05/1996
1         4         2       4        01/01/1997
1         6         2       4       *01/05/1996

任何帮助将不胜感激!

示例1(不起作用):

UPDATE ValInvData
SET ValStr = ValStr
WHERE ValInvData.FieldId = 11061
  AND ValInvData.ObjID = ValInvData.ObjId
  AND ValInvData.ObjInv = ValInvData.ObjInv
  AND ValInvData.FieldId != 11051
  AND ValInvData.FieldId != 11061
  AND ValInvData.FieldId != 11062
  AND ValInvData.FieldId != 11055
  AND ValInvData.FieldId != 11228
  AND ValInvData.FieldId != 11230
  AND ValInvData.FieldId != 14676;

示例2(不起作用):

UPDATE ValInvData
SET ValStr = (SELECT ValStr
              FROM ValInvData
              WHERE Valdata.FieldId = 11061
                AND ObjData.ObjID = ValData.ObjId
                AND ValInvData.ObjInv = ValInvData.ObjInv
                AND ValInvData.FieldId != 11051
                AND ValInvData.FieldId != 11061
                AND ValInvData.FieldId != 11062
                AND ValInvData.FieldId != 11055
                AND ValInvData.FieldId != 11228
                AND ValInvData.FieldId != 11230
                AND ValInvData.FieldId != 14676);

最终解决方案:

update ValInvData
set ValStr = coalesce(
    (
        select min(ValStr) -- 保证只返回一个值,
                            -- 可能最好抛出错误??
        from ValInvData as vid
        where vid.ObjId = ValInvData.ObjId and vid.ObjInv = ValInvData.ObjInv and vid.FieldId = 11061
    ),
    ValInvData.ValStr) -- 也可以使用exists子查询来确保匹配行可用
where FieldId = 11229;
英文:

For example the data I want to bring over resides in Table1, in column example_Date. The application in question uses this column to house data for multiple data field ID's.

Table1:

Table ID  Field ID  Obj ID  Obj Inv  example_Date
--------------------------------------------------
1         2         1       3        01/01/1996
1         4         1       3        01/02/1996
1         6         1       3        01/01/1996
1         2         2       4        01/05/1996
1         4         2       4        01/01/1997
1         6         2       4        10/01/1998

Then let's say I want to take the data in example_date that has a Field ID of 2 and copy it over to anything in the same table that has the following constraints:

  1. Obj ID = Obj ID
  2. Obj Inv = Obj Inv
  3. Field ID = 6

And the result would look like the below ( * = the updated data)

Table1:

Table ID  Field ID  Obj ID  Obj Inv  example_Date
-------------------------------------------------
1         2         1       3        01/01/1996
1         4         1       3        01/02/1996
1         6         1       3       *01/01/1996
1         2         2       4        01/05/1996
1         4         2       4        01/01/1997
1         6         2       4       *01/05/1996

Any help would be highly appreciated!

Example 1 (does not work):

UPDATE ValInvData
SET ValStr = ValStr
WHERE ValInvData.FieldId = 11061
  AND ValInvData.ObjID = ValInvData.ObjId
  AND ValInvData.ObjInv = ValInvData.ObjInv
  AND ValInvData.FieldId != 11051
  AND ValInvData.FieldId != 11061
  AND ValInvData.FieldId != 11062
  AND ValInvData.FieldId != 11055
  AND ValInvData.FieldId != 11228
  AND ValInvData.FieldId != 11230
  AND ValInvData.FieldId != 14676;

Example 2 (does not work):

UPDATE ValInvData
SET ValStr = (SELECT ValStr
              FROM ValInvData
              WHERE Valdata.FieldId = 11061
                AND ObjData.ObjID = ValData.ObjId
                AND ValInvData.ObjInv = ValInvData.ObjInv
                AND ValInvData.FieldId != 11051
                AND ValInvData.FieldId != 11061
                AND ValInvData.FieldId != 11062
                AND ValInvData.FieldId != 11055
                AND ValInvData.FieldId != 11228
                AND ValInvData.FieldId != 11230
                AND ValInvData.FieldId != 14676);

Final Solution

update ValInvData
set ValStr = coalesce(
    (
        select min(ValStr) -- to guarantee only one value returned,
                                 -- possibly better to error??
        from ValInvData as vid
        where vid.ObjId = ValInvData.ObjId and vid.ObjInv = ValInvData.ObjInv and vid.FieldId = 11061
    ),
    ValInvData.ValStr) -- could also use exists subquery to ensure matching row is available
where FieldId = 11229;

答案1

得分: 1

更新 ValInvData
设置 example_date = coalesce(
(
选择 min(example_date) -- 以确保只返回一个值,
-- 也许最好报错??
从 ValInvData 作为 vid
其中 vid.ObjId = ValInvData.ObjId 和 vid.ObjInv = ValInvData.ObjInv
并且 vid.FieldId = 2
),
example_date) -- 也可以使用存在子查询来确保匹配的行可用
其中 FieldId = 6;

英文:
update ValInvData
set example_date = coalesce(
    (
        select min(example_date) -- to guarantee only one value returned,
                                 -- possibly better to error??
        from ValInvData as vid
        where vid.ObjId = ValInvData.ObjId and vid.ObjInv = ValInvData.ObjInv
            and vid.FieldId = 2
    ),
    example_date) -- could also use exists subquery to ensure matching row is available
where FieldId = 6;

huangapple
  • 本文由 发表于 2023年7月11日 04:07:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定