SQL求解....关于去重,分组,聚合.

ldz2682 2011-03-24 08:26:52
我有两张表

用户表
id(PK) UserName userPhone userAddress
1 zhansan1 ph2oneNo ad1ress
2 zhansan2 pho2neNo adre3ss
3 zhansan3 phon3eNo ad3ress
4 zhansan4 pheoneNo adre2ss
5 zhansan51 phon1eNo adres3s

记录表
id(pk) userId(fk) money type tiem datail1 1 1000.00 收入 2011-03-23 15:35:54.403 ....
2 1 1000.00 收入 2011-03-23 15:35:54.403 ....
3 1 200.00 支出 2011-03-23 15:35:54.403 ....
4 1 800.00 记账 2011-03-23 15:35:54.403 ....
5 2 1000.00 收入 2011-03-23 15:35:54.403 ....
6 3 1000.00 收入 2011-03-23 15:35:54.403 ....
7 4 1234.00 记账 2011-03-23 15:35:54.403 ....
8 1 1000.00 收入 2011-03-23 15:35:54.403 ....


怎么写sql 才能查出来以下这样的效果呢

userId(或者userName) 总支出 总收入 总记账
xxxx xxx xxx xxx
xxxx xxx xxx xxx
xxxx xxx xxx xxx
xxxx xxx xxx xxx

SQL高手们帮我看看谢谢啦
...全文
170 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liang145 2011-03-25
  • 打赏
  • 举报
回复

create table #TableA
(id int, userId int,
[money] float, [type] nvarchar(10),
tiem datetime ,datail1 nvarchar(10))
insert #TableA select 1 ,2, 1000.00, '收入' ,'2011-03-23 15:35:54','403'union all
select 2, 1, 1000.00 , '收入' ,'2011-03-23 15:35:54','403'union all
select 3, 1 ,200.00 , '支出' ,'2011-03-23 15:35:54','403'union all
select 4 ,1 ,800.00 , '记账' ,'2011-03-23 15:35:54','403'union all
select 5 ,2 ,1000.00 , '收入' ,'2011-03-23 15:35:54','403'union all
select 6 ,3 ,1000.00 , '收入' ,'2011-03-23 15:35:54','403'union all
select 7 ,4, 1234.00 , '记账' ,'2011-03-23 15:35:54','403'union all
select 8 ,1 ,1000.00 , '收入' ,'2011-03-23 15:35:54','403'

select userId,
sum(case [type] when '收入' then [money] else 0 end)as '总收入',
sum(case [type] when '支出' then [money] else 0 end)as '总支出',
sum(case [type] when '记账' then [money] else 0 end)as '总记账'
from #TableA group by userId


case分支,如:
case [type] when '收入' then [money] else 0 end
表示当[type]值等'收入'时,返回[money]的值,否则返回0
ldz2682 2011-03-24
  • 打赏
  • 举报
回复
好用,,,可以讲下cese什么的么 我不大明白 谢谢了
--小F-- 2011-03-24
  • 打赏
  • 举报
回复
select
b.userid,
sum(case type when '支出' then [money] else 0 end) as 总支出,
sum(case type when '收入' then [money] else 0 end) as 总收入,
sum([money]) as 总记账
from
用户表 a, 记录表 b
where
a.id=b.userid
group by
b.userid
--小F-- 2011-03-24
  • 打赏
  • 举报
回复
seelct
b.userid,
sum(case type when '支出' then [money] else 0 end) as 总支出,
sum(case type when '收入' then [money] else 0 end) as 总收入,
sum([money]) as 总记账
from
用户表 a, 记录表 b
where
a.id=b.userid
group by
b.userid
dawugui 2011-03-24
  • 打赏
  • 举报
回复

--如果不匹配的记录不显示
select m.userId, m.userName ,
sum(case type when '支出' then money else 0 end) [总支出],
sum(case type when '收入' then money else 0 end) [总收入],
sum(case type when '记账' then money else 0 end) [总记账]
from 用户表 m , 记录表 n
where m.id = n.userId
group by m.userId, m.userName

--如果不匹配的记录显示为0
select m.userId, m.userName ,
sum(case type when '支出' then money else 0 end) [总支出],
sum(case type when '收入' then money else 0 end) [总收入],
sum(case type when '记账' then money else 0 end) [总记账]
from 用户表 m left join 记录表 n
on m.id = n.userId
group by m.userId, m.userName
dawugui 2011-03-24
  • 打赏
  • 举报
回复
select m.userId, m.userName , 
sum(case type when '支出' then money else 0 end) [总支出],
sum(case type when '收入' then money else 0 end) [总收入],
sum(case type when '记账' then money else 0 end) [总记账]
from 用户表 m , 记录表 n
where m.id = n.userId
group by m.userId, m.userName

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧