2015-03-31 55 views
0

i have database like this...SQL服务器数再行

how i can result like this ?

怎么可能会导致我这样? 在我看来,我可以查询=>

select id, count(no1, no2, no3) where no1='B',no2='B',no3='B' 

非常感谢。

回答

1

使用Case When陈述与Count骨料。最后,集团他们id

Select id, 
     count(case when no1='B' then 1 END) + 
     count(case when no2='B' then 1 END) + 
     count(case when no3='B' then 1 END) AS count_all 
From yourtable 
Group by id 
+0

的Rigels ...您的权利...〜_^ – SimanisJKT48 2015-03-31 02:20:11

+0

的欢迎^ _^ – Rigel1121 2015-03-31 02:26:30

+1

两个'Count'和'集团by'不需要这是一个不必要的开销 – 2015-03-31 02:34:35

3

使用Case Statement

select id, 
     case when no1='B' then 1 else 0 END + 
     case when no2='B' then 1 else 0 END + 
     case when no3='B' then 1 else 0 END As Count_All 
From yourtable 
+0

感谢的非常... Fireblade为感谢 – SimanisJKT48 2015-03-31 02:20:49