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

sql server update+select(子查询修改)20190304的更多相关文章
- Sql Server系列:子查询
1 子查询概念 子查询是嵌套在另一个查询中的普通T-SQL查询.在有一个SELECT语句通过使用小括号创建子查询,作为另一个查询的部分数据或条件的基础. 子查询通常用于满足以下某个需求: ◊ 将一个查 ...
- SQL Server 基础:子查询
1.子查询的概念:子查询就是嵌套在主查询中的查询.子查询可以嵌套在主查询中所有位置,包括SELECT.FROM.WHERE.GROUP BY.HAVING.ORDER BY.2.子查询的分类:2.1按 ...
- c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程
c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...
- Oracle\MS SQL Server Update多表关联更新
原文:Oracle\MS SQL Server Update多表关联更新 一条Update更新语句是不能更新多张表的,除非使用触发器隐含更新.而表的更新操作中,在很多情况下需要在表达式中引用要更新的表 ...
- SQL Server中SELECT会真的阻塞SELECT吗?
在SQL Server中,我们知道一个SELECT语句执行过程中只会申请一些意向共享锁(IS) 与共享锁(S), 例如我使用SQL Profile跟踪会话86执行SELECT * FROM dbo.T ...
- 第1/24周 SQL Server 如何执行一个查询
大家好,欢迎来到第1周的SQL Server性能调优培训.在我们进入SQL Server性能调优里枯燥难懂的细节内容之前,我想通过讲解SQL Server如何执行一个查询来建立基础.这个部分非常重要, ...
- Sql Server来龙去脉系列之三 查询过程跟踪
我们在读写数据库文件时,当文件被读.写或者出现错误时,这些过程活动都会触发一些运行时事件.从一个用户角度来看,有些时候会关注这些事件,特别是我们调试.审核.服务维护.例如,当数据库错误出现.列数据被更 ...
- 第1周 SQL Server 如何执行一个查询
原文:第1周 SQL Server 如何执行一个查询 大家好,欢迎来到第1周的SQL Server性能调优培训.在我们进入SQL Server性能调优里枯燥难懂的细节内容之前,我想通过讲解SQL Se ...
- 在SQL Server 2016里使用查询存储进行性能调优
作为一个DBA,排除SQL Server问题是我们的职责之一,每个月都有很多人给我们带来各种不能解释却要解决的性能问题. 我就多次听到,以前的SQL Server的性能问题都还好且在正常范围内,但现在 ...
随机推荐
- HTTP网络请求原理 (三) 简单模拟HTTP服务器
HTTP实际上是基于TCP的应用层协议,它在更高的层次封装了TCP的使用细节,是网络请求操作更为易用. TCP连接是因特网上基于流的可靠连接,它为HTTP提供了一条可靠的比特传输管道. 从TCP连接一 ...
- 【IOI 1998】 Picture
[题目链接] 点击打开链接 [算法] 线段树扫描线求周长并 [代码] #include <algorithm> #include <bitset> #include <c ...
- glyphicon 图标的使用
基本用法<span class="glyphicon glyphicon-search"></span>可以将图标放于按钮于链接中,一个大按钮,按钮的内容是 ...
- Properties 文件的简单操作
properties 文件里面主要 存 一个Key对应一个Value 一般用来存放账户密码资料 方法有:Properties p=new Properties(); p.setproperty(& ...
- P4323 [JSOI2016]独特的树叶(树哈希)
传送门 树哈希?->这里 反正大概就是乱搞--的吧-- //minamoto #include<bits/stdc++.h> #define R register #define l ...
- Matlab实现Butterworth滤波器 分类: 图像处理 2014-06-02 00:05 527人阅读 评论(0) 收藏
下面是用Matlab实现的Butterworth高通.低通滤波器. clc;clear all;close all; I=imread('cameraman.tif'); subplot(3,2,1) ...
- hibernate 中createQuery与createSQLQuery(转载)
息: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.miracle.dm.doc.catalog.mo ...
- 转-CoreText 使用教程
转自:http://www.dapps.net/dev/iphone/how-to-create-a-simple-magazine-app-with-core-text.html Core Text ...
- 转】SparkSQL中的内置函数
原博文来自于: http://blog.csdn.net/u012297062/article/details/52207934 感谢! 使用Spark SQL中的内置函数对数据进行分析,Spa ...
- 如何看Spring源码
想要深入的熟悉了解Spring源码,我觉得第一步就是要有一个能跑起来的极尽简单的框架,下面我就教大家搭建一个最简单的Spring框架,而且是基于Java Config形式的零配置Spring框架. 首 ...