3

我不知道如何用其他一些列的内容的行数来制作一列。不计算所有列 (*)

-------------------------    Output should be (NO COLUMN D):    ---------
|  A  |  B  |  C  |  D  |                                       | Count |
-------------------------                                       ---------
| foo | foo | foo | foo |                                       |   9   |
-------------------------                                       ---------
| bar | bar | bar | bar |
-------------------------
|  x  |  x  |     |  x  |
-------------------------
|  y  |     |     |  y  |
-------------------------

我尝试了很多方法,例如:

  • SELECT SUM(COUNT(A) + Count(B) + COUNT(C)) from SomeTable
  • SELECT COUNT(A + B + C)
  • SELECT COUNT(A,B,C)

我找不到正确的语法。有人可以帮我解决这个问题吗?

4

2 回答 2

5
SELECT SUM(CASE WHEN A IS  NULL THEN 0 ELSE 1 END) 
     + SUM(CASE WHEN B IS  NULL THEN 0 ELSE 1 END) 
     + SUM(CASE WHEN C IS  NULL THEN 0 ELSE 1 END) 
     AS [TOTAL]
FROM dbo.SomeTable

演示

于 2013-04-30T13:37:37.797 回答
1

昨天遇到同样的错误。这就是我解决它的方法。

Select Count(sum) from
(select 
count(A) as sum
from sometable
where ISNULL(A,'')<>''
,select 
count(B) as sum
from sometable
where ISNULL(B,'')<>''
,select 
count(C) as sum
from sometable
where ISNULL(C,'')<>''
)t
于 2013-04-30T13:36:58.633 回答