SQL查询到COUNT个字段,这些字段与表中所有行的特定值匹配

时间:2014-11-19 22:12:19

标签: sql sql-server tsql

我正在尝试(并且失败)制作一个简单的SQL查询(对于SQL Server 2012),它计算给定日期范围内值的出现次数。

这是调查结果的集合。

因此,最终结果将显示只有3个值匹配' 2'和 6个值匹配' 1'。

如果最终结果可以返回3个值,那就更好了:

MatchZero = 62
MatchOne = 6
MatchTwo = 3

喜欢的东西(我知道这很糟糕):

SELECT 
  COUNT(0) AS MatchZero, 
  COUNT(1) AS MatchOne, 
  COUNT(2) As MatchTwo 
WHERE dated BETWEEN '2014-01-01' AND '2014-02-01'

我不需要按日期或任何方式对其进行分组,只是每个的总值。

任何见解都会受到极大的欢迎。

+------------+----------+--------------+-------------+------+-----------+------------+
| QuestionId | friendly | professional | comfortable | rate | recommend |   dated    |
+------------+----------+--------------+-------------+------+-----------+------------+
|          3 |        0 |            0 |           0 |    0 |         0 | 2014-02-12 |
|          9 |        0 |            0 |           0 |    0 |         0 | 2014-02-12 |
|         14 |        0 |            0 |           0 |    2 |         0 | 2014-02-13 |
|         15 |        0 |            0 |           0 |    0 |         0 | 2014-01-06 |
|         19 |        0 |            1 |           2 |    0 |         0 | 2014-01-01 |
|         20 |        0 |            0 |           0 |    0 |         0 | 2013-12-01 |
|         21 |        0 |            1 |           0 |    0 |         0 | 2014-01-01 |
|         22 |        0 |            1 |           0 |    0 |         0 | 2014-01-01 |
|         23 |        0 |            0 |           0 |    0 |         0 | 2014-01-24 |
|         27 |        0 |            0 |           0 |    0 |         0 | 2014-01-31 |
|         30 |        0 |            1 |           2 |    0 |         0 | 2014-01-27 |
|         31 |        0 |            0 |           0 |    0 |         0 | 2014-01-11 |
|         36 |        0 |            0 |           0 |    1 |         1 | 2014-01-22 |
+------------+----------+--------------+-------------+------+-----------+------------+

3 个答案:

答案 0 :(得分:1)

您可以使用条件聚合:

SELECT SUM((CASE WHEN friendly = 0 THEN 1 ELSE 0 END) +
           (CASE WHEN professional = 0 THEN 1 ELSE 0 END) +
           (CASE WHEN comfortable = 0 THEN 1 ELSE 0 END) +
           (CASE WHEN rate = 0 THEN 1 ELSE 0 END) +
           (CASE WHEN recommend = 0 THEN 1 ELSE 0 END) +
          ) AS MatchZero,
       SUM((CASE WHEN friendly = 1 THEN 1 ELSE 0 END) +
           (CASE WHEN professional = 1 THEN 1 ELSE 0 END) +
           (CASE WHEN comfortable = 1 THEN 1 ELSE 0 END) +
           (CASE WHEN rate = 1 THEN 1 ELSE 0 END) +
           (CASE WHEN recommend = 1 THEN 1 ELSE 0 END) +
          ) AS MatchOne,
       SUM((CASE WHEN friendly = 2 THEN 1 ELSE 0 END) +
           (CASE WHEN professional = 2 THEN 1 ELSE 0 END) +
           (CASE WHEN comfortable = 2 THEN 1 ELSE 0 END) +
           (CASE WHEN rate = 2 THEN 1 ELSE 0 END) +
           (CASE WHEN recommend = 2 THEN 1 ELSE 0 END) +
          ) AS MatchTwo           
FROM . . .
WHERE dated BETWEEN '2014-01-01' AND '2014-02-01';

答案 1 :(得分:1)

如果我理解正确,您需要计算表格中特定(或每个)列的零,一和二。如果这是正确的,那么你可以这样做:

select sum(case when your_column = 0 then 1 else 0 end) as zeros
     , sum(case when your_column = 1 then 1 else 0 end) as ones
     --- and so on
from your_table
-- where conditions go here

如果您想计算多个列的总数,请在case...end中附上所需的sum()

sum(
    (case when column1 = 0 then 1 else 0 end) +
    (case when column2 = 0 then 1 else 0 end)
    -- and so on
) as zeros

答案 2 :(得分:0)

使用简单的unpivot,您可以通过更少的编码获得所需的结果。 只需更改日期范围,即可计算每种问题类型的正确计数。

SELECT 
    RANKING, COUNT(*) AS CNT 
FROM
    (SELECT 
        friendly,professional,comfortable,rate,recommend 
    FROM 
        your_table 
    WHERE 
        dated >= '1/1/1900' AND dated <= '1/1/2015'
) AS U UNPIVOT 
(RANKING FOR QUESTION IN (friendly,professional,comfortable,rate,recommend)) AS UNP
GROUP BY
    RANKING