如何计算col1&的每个组合关于col3

时间:2017-05-02 21:15:17

标签: sql sql-server-2012

表:我在SQL Server 2012中有一个数据库表table_name2,其数据为:

col1    col2    col3
one     first   abc-3ab9-cd-f-01
two     second  abcdefg04
three   first   
three   second  mnopqrs03
one     first   NULL
two     first   'NULL' 
one     second  papapap
two     second  dbrf434
two     first   434jhpo

DDL:并使用

创建
CREATE TABLE [dbo].[table_name2](
    [col1] [nvarchar](255) NULL,
    [col2] [nvarchar](255) NULL,
    [col3] [nvarchar](255) NULL
) ON [PRIMARY];

已填充数据:

insert into table_name2
values ('one','first','abc-3ab9-cd-f-01'),
('two','second','abcdefg04'),
('three','first',''),
('three','second','mnopqrs03'),
('one','first',NULL),
('two','first','NULL'),
('one','second','papapap'),
('two','second','dbrf434'),
('two','first','434jhpo');

我想列出col1和col2的每个组合。然后计算每个col1 / col2组合的col3数,其中col3为空白,“N / A”,“NULL”或“空白”。

对于col1 / col2的那些组合,其中没有满足与col3相关的条件,我想为这些组合显示0。

我想要的结果是:

col1    col2    MainCount
one     first   1
one     second  0
three   first   1
three   second  0
two     first   1
two     second  0

我尝试了什么:目前我尝试了一个简单的查询及其两个修改版本

SELECT col1, col2, count(col3) as MainCount
FROM table_name2
WHERE col3 is null OR col3 = '' OR col3 = 'N/A' OR col3 = 'Unknown' OR col3 = 'NULL'
GROUP BY col1, col2
ORDER BY col1, col2;

给出:

col1    col2    MainCount
one     first   0
three   first   1
two     first   1

,然后

SELECT col1, col2, count(col3) as MainCount
FROM table_name2
WHERE col3 =
    CASE WHEN
        (col3 is null OR col3 = '' OR col3 = 'N/A' OR col3 = 'Unknown' OR col3 = 'NULL')
         THEN col3 ELSE NULL
    END
GROUP BY col1, col2
ORDER BY col1, col2;

给出:

col1    col2    MainCount
three   first   1
two     first   1

最后一个

SELECT col1, col2, count(CASE WHEN col3 is not null THEN col3 ELSE 0 END) as MainCount  
FROM table_name2
WHERE col3 is null OR col3 = '' OR col3 = 'N/A' OR col3 = 'Unknown' OR col3 = 'NULL'
GROUP BY col1, col2
ORDER BY col1, col2;

给出错误:

Conversion failed when converting the nvarchar value 'NULL' to data type int.

我在这里做错了什么,如何获得所需的输出?

4 个答案:

答案 0 :(得分:3)

count中包含这些条件。

SELECT col1, col2,
count(case when col3 is null OR col3 = '' OR col3 = 'N/A' OR col3 = 'Unknown' OR col3 = 'NULL' then 1 end) as MainCount
FROM table_name2
GROUP BY col1, col2
ORDER BY col1, col2;

答案 1 :(得分:1)

select col1, col2
  , sum(case 
      when col3 is null then 1 
      when col3 in('','N/A','Unknown','NULL') then 1
      else 0
      end) as MainCount
from table_name2
group by col1, col2
order by col1, col2

rextester演示:http://rextester.com/QBOJ61516

返回:

+-------+--------+-----------+
| col1  |  col2  | MainCount |
+-------+--------+-----------+
| one   | first  |         1 |
| one   | second |         0 |
| three | first  |         1 |
| three | second |         0 |
| two   | first  |         1 |
| two   | second |         0 |
+-------+--------+-----------+

答案 2 :(得分:1)

这是因为col3不包含数字,但您在case语句中使用0。试试这个:

SELECT col1
,col2
,sum(CASE WHEN col3 is null THEN 1
          WHEN col3 in ('', 'N/A', 'Unknown', 'NULL') THEN 1
          ELSE 0 END) as MainCount  
FROM table_name2
GROUP BY col1, col2
ORDER BY col1, col2;

答案 3 :(得分:1)

尝试此操作,如有任何疑问,请与我联系。

select col1,col2,sum(case when col3 IS NULL OR COL3 in ('NULL', '', 'N/A') THEN 1 ELSE 0 END) AS MAINCOUNT from table_name2
group by col1,col2
ORDER BY COL1,COL2;