如何在 col3 的特定条件下获取 col1 和 col2 的每个组合的计数
Posted
技术标签:
【中文标题】如何在 col3 的特定条件下获取 col1 和 col2 的每个组合的计数【英文标题】:How to get count for each combination of col1 & col2 on specific conditions for col3 【发布时间】:2017-05-02 21:15:17 【问题描述】:表:我在 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 或'NULL'。
对于那些与 col3 相关的条件都不满足的 col1/col2 组合,我想为这些组合显示 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.
我在这里做错了什么,如何获得所需的输出?
【问题讨论】:
【参考方案1】:在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;
【讨论】:
感谢大家的意见。所有建议的解决方案都有效,但我接受 vkp 答案,因为它最接近我的方法并且首先发布。【参考方案2】: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 |
+-------+--------+-----------+
【讨论】:
【参考方案3】:这是因为 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;
【讨论】:
【参考方案4】:试试这个,如果有任何疑问,请告诉我。
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;
【讨论】:
以上是关于如何在 col3 的特定条件下获取 col1 和 col2 的每个组合的计数的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Oracle 8i 中指定可选的 OUTER JOIN 条件
如何在 Col3 中显示 Col1 值为 true 的值,而 Col1 中的 false 值仅在 Col3 中显示 NULL
SQL Server:查询以获取表 1 的 Col1 中的值的总和,以获取表 2 的 Col2 中的条件