日期:2014-05-16 浏览次数:20653 次
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-03-15 09:12:25
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([保单号] int,[保费] numeric(6,2))
insert [tb]
select 110001,3000.00 union all
select 110002,-2000.00 union all
select 110003,2000.00 union all
select 110004,-2000.00 union all
select 110004,2000.00 union all
select 110004,-2000.00 union all
select 110004,2000.00 union all
select 110005,2000.00 union all
select 110005,1000.00 union all
select 110006,-1000.00 union all
select 110006,1000.00
--------------开始查询--------------------------
;WITH f AS
(
SELECT 保单号,SUM(保费) AS num FROM tb GROUP BY 保单号
)
DELETE t FROM TB T WHERE EXISTS(SELECT 1 FROM f WHERE 保单号=t.保单号 AND num=0)
SELECT * FROM tb
----------------结果----------------------------
/* 保单号 保费
----------- ---------------------------------------
110001 3000.00
110002 -2000.00
110003 2000.00
110005 2000.00
110005 1000.00
*/
create table 表一
(保单号 varchar(10),保费 decimal(10,2))
insert into 表一
select '110001',3000.00 union all
select '110002',-2000.00 union all
select '110003',2000.00 union all
select '110004',-2000.00 union all
select '110004',2000.00 union all
s