首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求帮一个简单的查询,该怎么处理

2012-01-20 
求帮一个简单的查询表idfeetype15211110012220132251344011查询后实现idfeetype1152mixed2451344011算相同

求帮一个简单的查询


id fee type
1 52 11
1 100 12
2 20 13
2 25 13
4 40 11

查询后实现
id fee type
1 152 mixed
2 45 13
4 40 11

算相同id的fee 总数,并且如果type数>=2的时候显示mixed,要求一条语句



[解决办法]

SQL code
-->生成测试数据 declare @tb table([id] int,[fee] int,[type] int)Insert @tbselect 1,52,11 union allselect 1,100,12 union 2,
[解决办法]
SQL code
select id,sum(fee) as fee, case when (select count(1) from ta b where a.id=b.id group by b.type ) >=2then 'mixed' else count(1) end  as typefrom ta agroup by id
[解决办法]
SQL code
create table ta ([id] int,[fee] int,[type] int)Insert taselect 1,52,11 union allselect 1,100,12 union allselect 2,20,13 union allselect 2,25,13 union allselect 4,40,11select id,sum(fee) as fee,TYPE= case when count(1)>=2 then 'mixed' else LTRIM(max(type)) endfrom (select id,sum(fee) as fee,  typefrom ta agroup by id,type) aagroup by idid          fee         TYPE         ----------- ----------- ------------ 1           152         mixed2           45          134           40          11 

热点排行