Update和Select结合统计更新

update table_a set updatetime=getdate(), name=b.name

from (select name,age from table_b where table_b.Id=1) as b

where table_a.id=1

--

UPDATE A SET A1 = B1, A2 = B2, A3 = B3 FROM A, B WHERE A.ID = B.ID
update Table_Main set DataStatusId='902e0650-4fd2-432b-ad56-0750ba81f71e',
MonthClassHourSum=tmain.MonthClassHourSum,FTCNClassHourSum=tmain.FTCNClassHourSum,PTCNClassHourSum=tmain.PTCNClassHourSum,
FTENClassHourSum=tmain.FTENClassHourSum,PTENClassHourSum=tmain.PTENClassHourSum,
StaffClassHourSum=tmain.StaffClassHourSum, TakeClassHourSum=tmain.TakeClassHourSum,
TeachingTotalSum=tmain.TeachingTotalSum, TeachingClassSum=tmain.TeachingClassSum,
TeachingStudentSum=tmain.TeachingStudentSum, StudentLearnAge=tmain.StudentLearnAge,
TeachingPercent=tmain.TeachingPercent,TotalAttendStudentCnt=tmain.TeachingStudentSum, TotalClassTeacherCount=tmain.TotalClassTeacherCount,
TotalTeacherCount=tmain.TotalTeacherCount, GroupTeachItemCourse=tmain.GroupTeachItemCourse
from
(select sum(TeacherClassHour)as 'MonthClassHourSum',
sum(Case when TeacherTypeId='t1' then TeacherClassHour else 0 end) as 'FTCNClassHourSum',
sum(Case when TeacherTypeId='t2' then TeacherClassHour else 0 end) as 'PTCNClassHourSum',
sum(Case when TeacherTypeId='t3' then TeacherClassHour else 0 end) as 'FTENClassHourSum',
sum(Case when TeacherTypeId='t4' then TeacherClassHour else 0 end) as 'PTENClassHourSum',
sum(Case when TeacherTypeId='t5' then TeacherClassHour else 0 end) as 'StaffClassHourSum',
sum(Case when TeacherTypeId='t6' then TeacherClassHour else 0 end) as 'TakeClassHourSum',
COUNT(DISTINCT Id) as 'TeachingTotalSum',COUNT(DISTINCT TeachItemClassId) as 'TeachingClassSum',sum(AttendStudentCnt) as 'TeachingStudentSum',
Convert(decimal(18,2),sum(TeacherClassHour)/COUNT(DISTINCT TeacherUserID)) as 'StudentLearnAge',
100*Convert(decimal(18,2),COUNT(DISTINCT TeacherUserID)/(select count(DISTINCT IdCard) from Table_User)) as 'TeachingPercent',
COUNT(DISTINCT TeacherUserID) as 'TotalClassTeacherCount',(select count(DISTINCT IdCard) from Table_User) as 'TotalTeacherCount',
(select count(DISTINCT Id) from [dbo].[Table_Group] where FKMainId=@mainId) as 'GroupTeachItemCourse'
from [dbo].[Table_Detail] where FKTMainId=@mainId
) tmain
where Tabel_Main.Id=@mainId

SQL

Update和Select结合统计更新的更多相关文章

  1. [转]oracle update set select from 关联更新

    本文转自:http://blog.csdn.net/disiwei1012/article/details/52589181 http://www.blogjava.net/Jhonney/archi ...

  2. oracle update set select from 关联更新

    工作中有个需求,现在新表中有一些数据跟老表的基本一样,这样只需要把老表中数据搬到新表中就可以了,同时把不同的字段修改下数据即可,在修改字段时发现,需要指定一个条件,比如主键id,来修改某条记录,这样一 ...

  3. oracle学习笔记:update一整列 关联更新

    普通的 update 都是根据条件来对部分列的内容进行修改,用法如下: update temp_cwh_table set name = 'xxx' where id = 1; 假设现在有2张表:A. ...

  4. Oracle update和select 关联

    Oracle update和select 关联 目录 Oracle update和select 关联 1.介绍 2.解决方法 2.1.需求 2.2.错误演示 2.3.解决方法 1.介绍 本文主要向大家 ...

  5. select for update和select for update wait和select for update nowait的区别

    CREATE TABLE "TEST6" ( "ID" ), "NAME" ), "AGE" ,), "SEX ...

  6. select * from salgrade for update和select * from salgrade for update nowait区别

    1,select * from salgrade for update session1 session2 SQL> delete salgrade where grade=1; 1 row d ...

  7. JavaScript 实现textarea限制输入字数, 输入框字数实时统计更新,输入框实时字数计算移动端bug解决

    textarea称文本域,又称文本区,即有滚动条的多行文本输入控件,在网页的提交表单中经常用到.与单行文本框text控件不同,它不能通过maxlength属性来限制字数,为此必须寻求其他方法来加以限制 ...

  8. 浅谈select for update 和select lock in share mode的区别

    有些情况下为了保证数据逻辑的一致性,需要对SELECT的操作加锁.InnoDB存储引擎对于SELECT语句支持两种一致性的锁定读(locking read)操作. . SELECT …… FOR UP ...

  9. 2nd 词频统计更新

    词频统计更新 实现功能:从控制台输入文件路径,并统计单词总数及不重复的单词数,并输出所有单词词频,同时排序. 头文件 #include <stdio.h> #include <std ...

随机推荐

  1. SQL 先固定特殊的几行数据之外再按照某一字段排序方法(CASE 字段排序(CASE WHEN THEN)

    查询用户表的数据,管理员用户始终在最前面,然后再按照CreateTime排序: SELECT TOP * FROM [dbo].[User] WHERE ParentID = '**' ORDER B ...

  2. javascript加超链接

    JavaScript link 方法:给字符串加上超链接JavaScript link 方法link 方法返回使用 HTML a 标签属性定义的(斜体)字符串.其语法如下:str_object.lin ...

  3. Markdown 语法 (转载)

    Markdown 语法整理大集合2017   1.标题 代码 注:# 后面保持空格 # h1 ## h2 ### h3 #### h4 ##### h5 ###### h6 ####### h7 // ...

  4. python 之 数据库(字段的约束条件,表之间的关系)

    10.6 约束条件 10.61 not null .default create table t15( id int, name ) not null, sex enum('male','female ...

  5. 前端require代码抽离小技巧

    DEMO 文件目录结构 plugin.js // /CommonJS规范 // var exports = module.exports; exports.test = function () { c ...

  6. [BZOJ3681]Arietta(可持久化线段树合并优化建图+网络流)

    暴力建图显然就是S->i连1,i->j'连inf(i为第j个力度能弹出的音符),j'->T连T[j]. 由于是“某棵子树中权值在某区间内的所有点”都向某个力度连边,于是线段树优化建图 ...

  7. 初试牛刀:实时天气WebService

    1.引入WebService:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx 2.声明接口→界面获取值传入接口→接口返回值处理→ ...

  8. RabbitMQ 应用一

    (百度百科)MQ全称为Message Queue,消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递指的 ...

  9. 3_PHP表达式_4_PHP运算符

    以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. 3.4.1 算术运算符 <?php $num1 = -10; $num2 = -4; $num3 = $nu ...

  10. 使用 JS 来动态操作 css ,你知道几种方法?

    JavaScript 可以说是交互之王,它作为脚本语言加上许多 Web Api 进一步扩展了它的特性集,更加丰富界面交互的可操作性.这类 API 的例子包括WebGL API.Canvas API.D ...