table
id money tepy
001 100 1
001 150 2
001 150 3
002 100 2
003 100 1
003 100 3
004 200 1说明:当tepy=1、2时 money 相加,当tepy=3时 money 减
结果id money
001 100
002 100
003 0
004 200
id money tepy
001 100 1
001 150 2
001 150 3
002 100 2
003 100 1
003 100 3
004 200 1说明:当tepy=1、2时 money 相加,当tepy=3时 money 减
结果id money
001 100
002 100
003 0
004 200
解决方案 »
- 大家看一下这个sql server 是不是 180天后到期的
- 大家好,请问为什么我的img它src到一个gif图片,它却不能动
- 来捡分
- 有关Bookmark Lookup物理运算符
- 请问with TB as表用法
- 关于 SQL 2005 的几个问题
- 全文检索能不能实现检索两个表的列呢????
- 雪地在线裸跪求教:为什么我用DTS把excel表格导入到数据库中,excel里明明只是一条记录,倒进数据库里却有6条一模一样的记录(解决立马给
- 昨天去一个地方面试遇到这样一道题,请各位帮帮我答出来。
- 存储过程中where拼接
- 为sql字段插入uniqueidenifier 指定值
- JAVA连接数据库总是连接不上,请大家帮帮忙,下个星期交项目了。。
FROM TB
GROUP BY ID
from (select id,case when tepy in (1,2) then money else -money end as money from [table])k
group by id
select id,sum(case tepy when 3 then -money
else money end) money
from table
group by id
go
create table [t] ([id] varchar(3),[money] int,[tepy] int)
insert into [t]
select '001',100,1 union all
select '001',150,2 union all
select '001',150,3 union all
select '002',100,2 union all
select '003',100,1 union all
select '003',100,3 union all
select '004',200,1
select ID,
sum([money])[money]
from
(
select ID,
sum([money])[money]
from [t]
where tepy in (1,2)
group by ID
union all
select ID,
-sum([money])[money]
from [t]
where tepy =3
group by ID)t
group by ID
/*
ID money
---- -----------
001 100
002 100
003 0
004 200(4 個資料列受到影響)
*/