求一sql语句或函数。现在想取出所有正数相加大于2的,并且至少有一个大于2的记录

aqqzy 2007-01-24 11:31:48
现在有一个表
UserID
A int
B int
C int
D int
E int
F int
G int
H int
I int
J int
K int
L int
这样十五个字段。
A至L中的数值有正有负。现在想取出所有从A到L中的正数相加大于2的,并且A到L中至少有一个大于2的记录

不知大家明白没有。
比如说
UserID 1 2 3
A    2 1 2
B    1 1 1
C   -1 1 0
D    0 0 0
E    0 0 0
F    0 0 0
G    0 0 0
H    0 0 0
I    0 0 0
J    0 0 0
K    0 0 0
L    0 0 0

像上面这个表中,我只能取出UserID是1和3的记录


http://community.csdn.net/Expert/topic/5315/5315473.xml?temp=.446377
...全文
383 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
playwarcraft 2007-01-24
  • 打赏
  • 举报
回复
如果數據量不多的話,將表行轉列也不錯哈,可以直接sum,max
smart_zcg 2007-01-24
  • 打赏
  • 举报
回复
select table1.UserId,A,B,.....,L from table1 join (select UserId,A+B.....+L as r from table1 where A>0 and B>0.....and L>0 and r > 2) as t on table1.UserId = t.UserId

smart_zcg 2007-01-24
  • 打赏
  • 举报
回复
select UserId,A,B,.....,L from table1 join (select UserId,A+B.....+L as r from table1 where A>0 and B>0.....and L>0 and r > 2) as t on table1.UserId = t.UserId
smart_zcg 2007-01-24
  • 打赏
  • 举报
回复
select * from table1 join (select UserId,A+B...+L as r from table1 where A>0 and B>0...and L>0 and r > 2) as t on table1.UserId = t.UserId
冷箫轻笛 2007-01-24
  • 打赏
  • 举报
回复
select id from
(
select id,
(case when A > 0 then a else 0 end ) + (case when B > 0 then B else 0 end ) .....+ (case when L > 0 then L else 0 end ) as sumall,
(case when A > 2 then 1 else 0 end ) + (case when B > 2 then 1 else 0 end ) .....+(case when L > 0 then 1 else 0 end ) as sum2
from tablename
)t where sumall > 2 and sum2 >= 1

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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