22,209
社区成员
发帖
与我相关
我的任务
分享
USE tempdb
GO
IF OBJECT_ID('test') IS NOT NULL DROP TABLE test
create table test(u varchar(10), status int, num int)
go
insert into test values
('001', 1, 240),
('002', 2, 1000),
('001', 3, 400),
('002', 4, 1300),
('003', 4, 60),
('004', 5, 120),
('005', 3, 700)
go
select a.U,SUM(num) as num from test as a,
(select U from test where status =1 or status =2) as b
where a.u=b.u group by a.u
create table test(u varchar(10), status int, num int)
go
insert into test values
('001', 1, 240),
('002', 2, 1000),
('001', 3, 400),
('002', 4, 1300),
('003', 4, 60),
('004', 5, 120),
('005', 3, 700)
go
select u, SUM(num) num from test
where exists(select * from test x where x.status in(1,2) and test.u = x.u)
group by u
go
drop table test
go
(7 行受影响)
u num
---------- -----------
001 640
002 2300
(2 行受影响)