帮忙写个sql!!!!
时间:2011-12-20
来源:互联网
id socre type
----------------------------------
1 50 1
1 40 1
1 30 2
1 40 2
1 70 11
1 60 11
条件 : 如果表中存在tpye = 11 的则取 tpye = 11 的记录分数总和
如果表中不存在 tpye = 11 则取 type = 2 的记录分数总和
如果表中不存在 type = 2 则取 type = 1 的记录分数总和
作者: xiaobudian_ppmm 发布时间: 2011-12-20
declare @t table (id int,socre int,type int) insert into @t select 1,50,1 union all select 1,40,1 union all select 1,30,2 union all select 1,40,2 union all select 1,70,11 union all select 1,60,11 if(exists (select 1 from @t where type=11)) select * from @t where type=11 if(not exists (select 1 from @t where type=11)) begin if(not exists (select 1 from @t where type=2)) select * from @t where type=1 else select * from @t where type=2 end
作者: maco_wang 发布时间: 2011-12-20
作者: xiaobudian_ppmm 发布时间: 2011-12-20
SQL code
--> --> (Roy)生成測試數據 if not object_id('Tempdb..#T') is null drop table #T Go Create table #T([id] int,[socre] int,[type] int) Insert #T select 1,50,1 union all select 1,40,1 union all select 1,30,2 union all select 1,40,2 union all select 1,70,11 union all select 1,60,11 Go --如果表中存在tpye = 11 的则取 tpye = 11 的记录分数总和 -- 如果表中不存在 tpye = 11 则取 type = 2 的记录分数总和 -- 如果表中不存在 type = 2 则取 type = 1 的记录分数总和 Select [type],[socre]=SUM([socre]) from #T where [type]=11 group by [type] union all Select [type],[socre]=SUM([socre]) from #T where not exists(select 1 from #T where [type]=11) and [type]=2 group by [type] union all Select [type],[socre]=SUM([socre]) from #T where not exists(select 1 from #T where [type] in(2,11)) and [type]=1 group by [type]
作者: roy_88 发布时间: 2011-12-20
作者: roy_88 发布时间: 2011-12-20
SQL code
select top 1 sum(score) from tb group by type order by type desc
作者: SQLCenter 发布时间: 2011-12-21
--楼主是要分组合计,还是按条件合计的 if object_id('tb') is not null drop table tb go create table tb ( id int, socre int, type int ) go insert into tb select 1,50,1 union all select 1,40,1 union all select 1,30,2 union all select 1,40,2 union all select 1,70,11 union all select 1,60,11 go if exists(select 1 from tb where type=11) select sum(socre) from tb where type=11 else if exists(select 1 from tb where type=2) select sum(socre) from tb where type=2 else if exists(select 1 from tb where type=1) select sum(socre) from tb where type=1 go /* ----------- 130 (1 行受影响) */
作者: pengxuan 发布时间: 2011-12-21
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28