SQL 如果存在就更新,如果不存在就添加,使用 Merge 函数(SQL2008版本及以上)
USE [NationalUnion]
GO
/****** Object: StoredProcedure [dbo].[proc_DataSummary] Script Date: 07/03/2014 15:33:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter proc [dbo].[proc_DataSummary20140703]
(
@UserID bigint--,
--@Level int
)
as
begin
begin try
begin tran
declare @Level int
set @Level=1 --默认Level为1为最后一级分享者
----------------------------------------PV---------------------------------------------------------------------------------------------------------------------------------
--PV
merge into dbo.DataSummary as DS
using (select SharedUserID,PlatformID,CONVERT(varchar(100), CreateDate, 111) as CreatDate,ChannelID,SharedManagerID,COUNT(*) as Qty from dbo.PVInfo where SharedLevel = @Level and (@UserID<0 or SharedUserID = @UserID) group by SharedUserID,PlatformID,ChannelID,SharedManagerID,CONVERT(varchar(100), CreateDate, 111)) as P
on (DS.UserID=P.SharedUserID and DS.PlatformID=P.PlatformID and DS.ChannelID=P.ChannelID and DS.ManagerID=P.SharedManagerID and DS.SummaryDate=P.CreatDate)
when matched then
update set PV=(PV+P.Qty)--更新统计己存在的信息
when not matched then--统计新的PV信息
insert values(P.SharedUserID,P.PlatformID,P.CreatDate,P.ChannelID,P.SharedManagerID,P.Qty,0,0,0,0,0,0,GETDATE());
----------------------------------------UV---------------------------------------------------------------------------------------------------------------------------------
--UV
merge into dbo.DataSummary as DS
using (select SharedUserID,PlatformID,CONVERT(varchar(100), CreateDate, 111) as CreatDate,ChannelID,SharedManagerID,COUNT(*) as Qty from dbo.UVInfo where SharedLevel = @Level and (@UserID<0 or SharedUserID = @UserID) group by SharedUserID,PlatformID,ChannelID,SharedManagerID,CONVERT(varchar(100), CreateDate, 111)) as U
on (DS.UserID=U.SharedUserID and DS.PlatformID=U.PlatformID and DS.ChannelID=U.ChannelID and DS.ManagerID=U.SharedManagerID and DS.SummaryDate=U.CreatDate)
when matched then
update set UV=(UV+U.Qty)--更新统计己存在的信息
when not matched then--统计新的PV信息
insert values(U.SharedUserID,U.PlatformID,U.CreatDate,U.ChannelID,U.SharedManagerID,0,U.Qty,0,0,0,0,0,GETDATE());
----------------------------------------预计可算拥金---------------------------------------------------------------------------------------------------------------------------------
merge into dbo.DataSummary as DS
using (select SharedUserID,PlatformID,CONVERT(varchar(100), CreateTime, 111) as CreatDate,ChannelID,SharedManagerID,sum(Commission) as TotalEstimateCommission from dbo.CPSOriDataOccur group by SharedUserID,PlatformID,ChannelID,SharedManagerID,CONVERT(varchar(100), CreateTime, 111)) as O
on (DS.UserID=O.SharedUserID and DS.PlatformID=O.PlatformID and DS.SummaryDate=O.CreatDate and DS.ChannelID=O.ChannelID and DS.ManagerID=O.SharedManagerID)
when matched then--预计可算拥金
update set ds.CommissionEstimate=(ds.CommissionEstimate+O.TotalEstimateCommission)
when not matched then
insert values( O.SharedUserID,O.PlatformID,O.CreatDate,O.ChannelID,O.SharedManagerID,0,0,0,0,O.TotalEstimateCommission,0,0,GETDATE());
----------------------------------------可结算拥金---------------------------------------------------------------------------------------------------------------------------------
merge into dbo.DataSummary as DS
using (select SharedUserID,PlatformID,CONVERT(varchar(100), CreateTime, 111) as CreatDate,ChannelID,SharedManagerID,SUM(Price) as TotalPrice,sum(Commission) as TotalCommission from dbo.CPSOriDataEffect group by SharedUserID,PlatformID,ChannelID,SharedManagerID,CONVERT(varchar(100), CreateTime, 111)) as E
on (DS.UserID=E.SharedUserID and DS.PlatformID=E.PlatformID and DS.SummaryDate=E.CreatDate and DS.ChannelID=E.ChannelID and DS.ManagerID=E.SharedManagerID)
when matched then
update set ds.OrderAmount=(ds.OrderAmount+E.TotalPrice),ds.AvaliableCommission=(ds.CommissionEstimate+E.TotalCommission)
when not matched then
insert values( E.SharedUserID,E.PlatformID,E.CreatDate,E.ChannelID,E.SharedManagerID,0,0,0,E.TotalPrice,0,E.TotalCommission,0,GETDATE()); commit tran
end try
begin catch
rollback tran
end catch
end
SQL 如果存在就更新,如果不存在就添加,使用 Merge 函数(SQL2008版本及以上)的更多相关文章
- SQL中使用UPDATE更新数据时一定要记得WHERE子句
我们在使用 SQL 中的 UPDATE 更新数据时,一般都不会更新表中的左右数据,所以我们更新的数据的 SQL 语句中会带有 WHERE 子句,如果没有WHERE子句,就回更新表中所有的数据,在 my ...
- 数据库:sql 多表联合更新【转】
SQL Update多表联合更新的方法 (1) sqlite 多表更新方法 update t1 set col1=t2.col1 from table1 t1 inner join table2 t2 ...
- Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例
如何用一条sql语句实现批量更新?mysql并没有提供直接的方法来实现批量更新,但是可以用点小技巧来实现. 复制代码 代码如下: UPDATE mytable SET myfield = CASE i ...
- sql查询,如果有更新时间则按更新时间倒序,没有则按创建时间倒序排列
原文:sql查询,如果有更新时间则按更新时间倒序,没有则按创建时间倒序排列 ORDER BY IFNULL(update_time,create_time) DESC IFNULL(expr1,exp ...
- 使用SQL中的update更新多个字段值
使用SQL中的update更新多个字段值,set后面的条件要用逗号不能用and set后面的多个条件之间没有关联也不可以有关联,所以就不能用and了:where 条件后面 可以为and 如: upda ...
- sql语法复习:增删查改,各种数据库对象创建和函数使用
推荐工具:机子配置较低的话,可以装Gsql这个工具获得sql执行环境(可作为手册查看内置数据类型 函数和存储过程等) --之前数据库的东西接触不多,虽然基本的语法是了解,但不是很熟悉--最近项目一直在 ...
- Oracle存在则更新,不存在则插入应用-merge
转: Oracle存在则更新,不存在则插入应用-merge 2017年01月11日 14:15:26 周星猩 阅读数 11354更多 分类专栏: Oracle 版权声明:本文为博主原创文章,遵循C ...
- 【SQL Server学习笔记】Delete 语句、Output 子句、Merge语句
原文:[SQL Server学习笔记]Delete 语句.Output 子句.Merge语句 DELETE语句 --建表 select * into distribution from sys.obj ...
- SQL Server 2008空间数据应用系列四:基础空间对象与函数应用
原文:SQL Server 2008空间数据应用系列四:基础空间对象与函数应用 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server 2008 R2调测. ...
随机推荐
- TCP/IP四层模型和OSI七层模型的概念
转:http://blog.csdn.net/superjunjin/article/details/7841099/ TCP/IP四层模型 TCP/IP是一组协议的代名词,它还包括许多协议,组成了T ...
- 不用配置tnsnames.ora,直接通过PL/SQL访问远程数据库
- 四层LB和七层LB
总结: 基于MAC地址玩的是二层(虚拟MAC地址接收请求,然后再分配到真实的MAC地址), 基于IP地址玩的是三层(虚拟IP地址接收请求,然后再分配到真实的IP地址), 基于IP地 ...
- CE_现金账户转账汇入汇出交易(案例)(未完成)
2014-07-15 BaoXinjian 一.摘要 二.案例 通过 Oracle Payments 结算事务处理 通过付款模板 事物处理子类型 已验证 -> 正在结算中 ->
- python(13)线程池:threading
先上代码: pool = threadpool.ThreadPool(10) #建立线程池,控制线程数量为10 reqs = threadpool.makeRequests(get_title, da ...
- oProfile 学习
oProfile工具可以分析CPU的负载量 只要对目标程序加上 -g 后重新编译,即可用oProfile进行分析 例如在测试apache的性能时, 增加 -g 编译选项[crifan@localhos ...
- 新找到的一款字体 fantasque-sans-mono
http://www.ipreferjim.com/2015/03/your-ides-font-matters-fantasque-sans-mono/
- [Flex] IFrame系列 —— 在flex的web应用中嵌入html的方法
在flex的web应用中,我们往往必须有嵌入html的需求,这时候你会发现IFrame很有用! flex而且可以和html中的JavaScript进行交互,flex可以通过iframe调用到html中 ...
- 华硕X84L无线驱动查找
打开官网:http://www.asus.com.cn/ 点击导航栏的服务与支持 产品型号识别http://www.asus.com.cn/support/Article/565/ 我的是:X84L ...
- [SQL]insert、update 表触发器应用的demo
--创建测试表 create table student ( stu_id int ,libraryCardNo varchar() ) create table borrowbook ( b_id ...