程序问答   发布时间:2022-05-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何编写此更新查询?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何编写此更新查询??

开发过程中遇到如何编写此更新查询?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何编写此更新查询?的解决方法建议,希望对你解决如何编写此更新查询?有所启发或帮助;

我在一个表上运行这个查询:

SELECT disTinCT product_type as pdt_typ,skill  
FROM [dbo].[Call_Data] 
WHERE skill NOT liKE '%HUA%'
AND product_type IS NOT NulL
ORDER BY skill ASC

这将返回以下记录:

pdt_typ     skill
HUA         1004
HUA         1007
HUA         1010
HUA         1020
C3_SandBox  1020
HUA         1024
HUA         1025
HUA         1026
HUA         1028
HUA         1029
HUA_Oxford  1031
HUA         1031
C3_SandBox  1031
HUA         1038
HUA_Oxford  1038
C3_SandBox  1038
HUA         1039
HUA         1040
HUA         1043
HUA         1046
C3_SandBox  1048
HUA         1048

如果我从 AND product_type IS NulL 子句中删除 WHERE,结果将是:

pdt_typ     skill
HUA         1004
NulL        1004
HUA         1007
HUA         1010
HUA         1020
C3_SandBox  1020
NulL        1020
HUA         1024
HUA         1025
HUA         1026
NulL        2016
HUA         1028
HUA         1029
HUA_Oxford  1031
HUA         1031
C3_SandBox  1031
NulL        1031
HUA         1038
HUA_Oxford  1038
C3_SandBox  1038
NulL        1038
HUA         1039
HUA         1040
HUA         1043
HUA         1046
C3_SandBox  1048
HUA         1048
NulL        1048

我需要更新上表。例如:`

updatE [dbo].{Call_Data] 
SET product_type = (
    SELECT product_type 
    FROM [dbo].[Call_Data]
    WHERE product_type IS NOT NulL 
    AND skill = 1004) 
WHERE product_type IS NulL
-- AND skill = 1004. use skills that have one product_type (not including the NulL product_typE) but 
-- do not use skill = 1020,1031,1038 or 1048 because those skill IDs have 2 or more product_types 
-- (not including the NulL product_typE).

我需要这部分的帮助。如何仅选择具有单个 product_type 的技能并使用该技能的 product_type 更新 NulL product_type。示例:

    pdt_typ     skill
    HUA         1004
    NulL        1004

`现在应该变成:

pdt_typ     skill
    HUA        1004
    HUA        1004

sql Server 2016。

提前致谢!

解决方法

需要关联子查询,并使用HAVING限制结果

updatE c1
SET product_type = c2.product_type
FROM [dbo].[Call_Data] c1
CROSS APPLY(
    SELECT product_type 
    FROM [dbo].[Call_Data] c2
    WHERE c2.product_type IS NOT NULL
        AND c2.skill = c1.skill
    GROUP BY product_type
    HAVING COUNT(*) = 1
) c2
WHERE product_type IS NULL

大佬总结

以上是大佬教程为你收集整理的如何编写此更新查询?全部内容,希望文章能够帮你解决如何编写此更新查询?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: