SQL Server统计Table里值不为0的列数

x287634334 2016-01-31 04:13:13
各位大神,咨询一个问题:
假如我这个表里有字段a1,a2,a3, a4,a5,a6这几个字段,其值分别是1,0,0,5,0,0
如本例,值不为0的列数是2。
那么如何统计出这个表里值不等于0的列数?
...全文
304 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2016-01-31
  • 打赏
  • 举报
回复
SELECT *, SIGN(a1)+SIGN(a2)+SIGN(a3)+SIGN(a4)+SIGN(a5)+SIGN(a6) AS [>0]
FROM tableName 
卖水果的net 2016-01-31
  • 打赏
  • 举报
回复


create table test(a1 int,a2 int,a3 int,a4 int,a5 int,a6 int)
go
insert into test values(1,0,0,5,0,0)
go
select  case when a1 !=0 then 1 else 0 end + 
        case when a2 !=0 then 1 else 0 end + 
        case when a3 !=0 then 1 else 0 end + 
        case when a4 !=0 then 1 else 0 end + 
        case when a5 !=0 then 1 else 0 end + 
        case when a6 !=0 then 1 else 0 end 
from test 
go
drop table test 
go


(1 行受影响)

-----------
2

(1 行受影响)


22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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