SQL 实现,如果存在就更新,如果不存在就添加
alter proc proc_DataSummary
as
begin
begin try
begin tran
--PV
--统计的信息存入临时表
select SharedUserID,PlatformID,CONVERT(varchar(100), CreateDate, 111) as CreatDate,ChannelID,SharedManagerID,COUNT(*) as Qty into #PVData from dbo.PVInfo where SharedLevel=1 group by SharedUserID,PlatformID,ChannelID,SharedManagerID,CONVERT(varchar(100), CreateDate, 111)
--更新将统计己存在的信息
update dbo.DataSummary set PV=dbo.#PVData.Qty from #PVData
where dbo.DataSummary.UserID=dbo.#PVData.SharedUserID and dbo.DataSummary.PlatformID=dbo.#PVData.PlatformID and dbo.DataSummary.ChannelID=dbo.#PVData.ChannelID
and dbo.DataSummary.ManagerID=dbo.#PVData.SharedManagerID and dbo.DataSummary.SummaryDate=dbo.#PVData.CreatDate
--统计新的PV信息
insert into dbo.DataSummary
select SharedUserID,PlatformID,CreatDate,ChannelID,SharedManagerID,Qty,0,0,0,0,0,0,GETDATE() from dbo.#PVData a
where not exists(select * from dbo.DataSummary where dbo.DataSummary.UserID=a.SharedUserID and dbo.DataSummary.PlatformID=a.PlatformID and dbo.DataSummary.ChannelID=a.ChannelID
and dbo.DataSummary.ManagerID=a.SharedManagerID and dbo.DataSummary.SummaryDate=a.CreatDate ) --UV
--统计的信息存入临时表
select SharedUserID,PlatformID,CONVERT(varchar(100), CreateDate, 111) as CreatDate,ChannelID,SharedManagerID,COUNT(*) as Qty into #UVData from dbo.UVInfo where SharedLevel=1 group by SharedUserID,PlatformID,ChannelID,SharedManagerID,CONVERT(varchar(100), CreateDate, 111)
--更新将统计己存在的信息
update dbo.DataSummary set UV=dbo.#UVData.Qty from #UVData
where dbo.DataSummary.UserID=dbo.#UVData.SharedUserID and dbo.DataSummary.PlatformID=dbo.#UVData.PlatformID and dbo.DataSummary.ChannelID=dbo.#UVData.ChannelID
and dbo.DataSummary.ManagerID=dbo.#UVData.SharedManagerID and dbo.DataSummary.SummaryDate=dbo.#UVData.CreatDate
--统计新的UV信息
insert into dbo.DataSummary
select SharedUserID,PlatformID,CreatDate,ChannelID,SharedManagerID,Qty,0,0,0,0,0,0,GETDATE() from dbo.#UVData a
where not exists(select * from dbo.DataSummary where dbo.DataSummary.UserID=a.SharedUserID and dbo.DataSummary.PlatformID=a.PlatformID and dbo.DataSummary.ChannelID=a.ChannelID
and dbo.DataSummary.ManagerID=a.SharedManagerID and dbo.DataSummary.SummaryDate=a.CreatDate ) commit tran
end try
begin catch
rollback tran
end catch
end
SQL 实现,如果存在就更新,如果不存在就添加的更多相关文章
- SQL SERVER 2008 R2 SP1更新时,遇上共享功能更新失败解决方案
SQL SERVER 2008 R2 SP1更新时,遇上共享功能更新失败的问题,可作如下尝试: 更新失败后,在windows的[事件查看器→应用程序]中找到来源为MsiInstaller,事件ID为1 ...
- 170823、SQL Update多表联合更新的方法
SQL Update多表联合更新的方法 (1) sqlite 多表更新方法 update t1 set col1=t2.col1 from table1 t1 inner join table2 t2 ...
- SQL Update多表联合更新的方法
SQL Update多表联合更新的方法 (1) sqlite 多表更新方法 update t1 set col1=t2.col1 from table1 t1 inner join table2 t2 ...
- 用SQL语句创建和删除Access数据库中的表;添加列和删除列
用SQL语句创建和删除Access数据库中的表;添加列和删除列 Posted on 2009-08-11 13:42 yunbo 阅读(1240) 评论(0) 编辑 收藏 用SQL语句创建和删除Acc ...
- 在SQL Server 2014里可更新的列存储索引 (Updateable Column Store Indexes)
传统的关系数据库服务引擎往往并不是对超大量数据进行分析计算的最佳平台,为此,SQL Server中开发了分析服务引擎去对大笔数据进行分析计算.当然,对于数据的存放平台SQL Server数据库引擎而言 ...
- sql 批量操作(存在的更新,不存在的插入)
标签: sql 2012-09-06 18:13 2408人阅读 评论(0) 收藏 举报 分类: Sql Server(123) 版权声明:本文为博主原创文章,未经博主允许不得转载. update A ...
- SQL入门学习3-数据更新
4-1 数据的插入(INSERT语句的使用方法) 使用INSERT语句可以向表中插入数据(行).原则上,INSERT语句背刺执行一行数据插入. CREATE TABLE 和INSERT 语句,都可以设 ...
- SQL随着子查询结果更新多个字段
笔者:iamlasong 要求:表格内容需要改变,在临时表中内容的变化,使用SQL官方声明更新表若干领域. 假设更新一个字段,直接用字段名=子查询就能够了,多个字段更新,将字段在括号里并列写出就可以, ...
- SQL入门(1): 创建/查询/更新/连接/视图/SSMS简介
本文介绍SQL的基本查询语句 (1) select... from * 表示全部, 选择的东西还可以进行简单的运算, 可以列别名 select * from student; -sage from ...
- 【SQL模板】三.插入/更新 数据模板TSQL
---Name: 插入/更新 数据模板.sql ---Purpose: 用于更新 数据库中 历史数据 或 插入 新数据 的脚本模板 ---Author: xx ---Time: 2015-12-18 ...
随机推荐
- 【转】页面跳转Transfer与Redirect的区别你知道吗?
一 前言 关于页面跳转的方式常用的应该就是,链接跳转,js跳转,Server.Tranfser和Response.Redirect 这几种,可是在Tranfser与Redirect之间用哪种更好(本文 ...
- JavaScript-CheckBox全选/反选
//------------------------------------ // 全/反选 // param checkName checkbox的name属性 //---------------- ...
- AP_AP系列 - 供应商管理(案例)
2014-07-03 Created By BaoXinjian
- Bi-shoe and Phi-shoe(欧拉函数)
题意: 给一些数Ai(第 i 个数),Ai这些数代表的是某个数欧拉函数的值,我们要求出数 Ni 的欧拉函数值不小于Ai.而我们要求的就是这些 Ni 这些数字的和sum,而且我们想要sum最小,求出su ...
- POJ 2411 Mondriaan'sDream(状压DP)
题目大意:一个矩阵,只能放1*2的木块,问将这个矩阵完全覆盖的不同放法有多少种. 解析:如果是横着的就定义11,如果竖着的定义为竖着的01,这样按行dp只需要考虑两件事儿,当前行&上一行,是不 ...
- Js读写cookie实例
统计访问次数 <html> <head> <meta http-equiv="Content-Type" content="text/htm ...
- DTD限制XML文件
需要通过使用Microsoft Visual Studio 2010.XMLSpy或者eclipse软件来检查校验 文件中有中文,需要存为utf-8编码 book.dtd <!ELEMENT 书 ...
- windows service自动启动相关设置
http://blog.csdn.net/thebesttome/article/details/7590025 原帖:http://www.cnblogs.com/junior/archive/ ...
- Haproxy安装及配置(转)
1.安装 # wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz # tar zcvf haproxy-1.3.20.t ...
- JAVA错误:Cannot refer to a non-final variable * inside an inner class defined in a different method
在使用Java局部内部类或者内部类时,若该类调用了所在方法的局部变量,则该局部变量必须使用final关键字来修饰,否则将会出现编译错误“Cannot refer to a non-final vari ...