• sql server update+select(子查询修改)20190304


    if OBJECT_ID('tempdb..##t2') is not null drop table ##t2;
    create table ##t2
    (
    a int,
    b int,
    c datetime,
    d varchar(100),
    e varchar(100),
    f int,
    g int
    );
    select * from ##t2;

    update ##t2
    set f = t3.f,
    g = t3.g
    FROM ##t2,(select f.NID,count(s.NID) skuCount,sum(s.Qty) skuSum
    from table1(nolock) t1,table2(nolock) t2
    where f.NID = s.nid
    group by f.NID) t3
    where ##t2.b = t3.NID;
    go

    --TestDemo
    if OBJECT_ID('tempdb..##t1') is not null drop table ##t1;
    create table ##t1
    (
        id int
    );
    if OBJECT_ID('tempdb..##t2') is not null drop table ##t2;
    create table ##t2
    (
        id int,
        id2 int,
        quantity int
    );
    if OBJECT_ID('tempdb..##t3') is not null drop table ##t3;
    create table ##t3
    (
        id int,
        id3 int,
        quantity2 int
    );
    insert into ##t1 values(1);
    insert into ##t1 values(2);
    insert into ##t1 values(3);
    insert into ##t1 values(4);
    insert into ##t1 values(5);
    go
    insert into ##t2 values(1,1,1);
    insert into ##t2 values(2,1,2);
    insert into ##t2 values(3,1,3);
    insert into ##t2 values(4,2,4);
    insert into ##t2 values(5,2,5);
    go
    insert into ##t3 values(1,1,6);
    insert into ##t3 values(2,1,7);
    insert into ##t3 values(3,2,8);
    insert into ##t3 values(4,2,9);
    insert into ##t3 values(5,2,10);
    go
    if OBJECT_ID('tempdb..##t4') is not null drop table ##t4;
    create table ##t4
    (
        id1 int,
        id2 int,
        quantityCount int,
        quantitySum int
    );
    go
    insert into ##t4(id1,id2)
    select t1.id,t2.id
    from ##t1 t1,##t2 t2
    where t1.id = t2.id2;
    update ##t4
    set quantityCount = t1.quantityCount,
    quantitySum = t1.quantitySum
    FROM ##t1,(select t2.id,
    count(t3.id) quantityCount,sum(t3.quantity2) quantitySum 
    from ##t2 t2,##t3 t3
    where t2.id = t3.id3
    group by t2.id) t1
    where ##t4.id2 = t1.id;
    go
    select * from ##t4;
    go

    2015年10月-2016年3月 总计:5个月.
    2016年11月-2017年6月 总计:7个月.
    2017年7月-2018年4月 总计:9个月.
    2018年5月-2018年5月 总计:1个月.
    2018年6月-2018年12月 总计:6个月.
    2019年1月-2019年12月 总计11个月.
    2020年2月-2021年2月 总计13个月.
    所有总计:5+7+9+1+6+11+13=52个月(4年4个月).
    本人认同二元论.我是理想主义者,现实主义者,乐观主义者,有一定的完美主义倾向.不过,一直都是咸鱼(菜鸟),就算有机会,我也不想咸鱼翻身.(并不矛盾,因为具体情况具体分析)
    英语,高等数学,考研,其他知识学习打卡交流QQ群:946556683
  • 相关阅读:
    字典序
    分数线划定
    聪明的质检员
    月考
    山头狙击战
    如何查看屏幕touch driver是否在正常工作
    python 核心编程课后练习(chapter 6)
    python 核心编程课后练习(chapter 5)
    python 核心编程课后练习(chapter 3)
    python 核心编程课后练习(chapter 2)
  • 原文地址:https://www.cnblogs.com/JimmySeraph/p/10469055.html
Copyright © 2020-2023  润新知