在线等----表中一个字段oderflag可能取值0,1,2,如何判断符合某一条件(肯定不是一条)的记录的oderflag全不为0?

duckcn 2002-08-21 02:48:22
不希望用循环一条条判断的方法,应该有其他简单的办法。
也想过用sum(orderflag)和count(*)比较,但没做出来。
...全文
43 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
duckcn 2002-08-21
  • 打赏
  • 举报
回复
有人帮忙解决了,挺简单的。

哎,自己搞晕了。

全不为零
select count(*) from table where condition and orderflag = 0

全为1
select count(*) from table where condition and orderflag <> 1

都是判断count(*)是否为零


散分!
supsuccess 2002-08-21
  • 打赏
  • 举报
回复
思路: 各orderflag相乘=1
icevi 2002-08-21
  • 打赏
  • 举报
回复
select id
from yourtable
group by id
having sum(case when orderflag=1 then 1 else 0 end )=count(orderflag)
蓝天 2002-08-21
  • 打赏
  • 举报
回复
select x.col1 ,x.col2 ,x.col3 ... from (select distinct col1,col2,col3 .... from yourtable where oderflag <> 0 ) x where not exists (select 1 from yourtable y where y.oderflag =0 and y.col1=x.col1 and y.col2=x.col2 and y.col3 =x.col3...)
ItSeeker 2002-08-21
  • 打赏
  • 举报
回复
select @num=count(*) from table where condition='..' and oderflag<>0
select @num1=count(*) from table where condition='..'
if (@num=@num1)
print '全不为0'
duckcn 2002-08-21
  • 打赏
  • 举报
回复
搞错了,应该是oderflag全为1。
蓝天 2002-08-21
  • 打赏
  • 举报
回复
select x.col1 ,x.col2 ,x.col3 ... from (select distinct col1,col2,col3 .... from yourtable where oderflag <> 0 ) x where not exists (select 1 from yourtable y where oderflag =0 and y.col1=x.col1 and y.col2=x.col2 and y.col3 =x.col3...)

34,587

社区成员

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

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