[英]How to Select count() inside select statement
After hours of trying, i have finally come to terms i need assistance. 经过数小时的尝试,我终于同意我需要帮助。
I'm trying to select a row from TableA and then count the number of cells of that row that are not empty. 我正在尝试从TableA中选择一行,然后计算该行中不为空的单元格的数量。
I know this is faulty but it communicates my intention 我知道这是有问题的,但这传达了我的意图
SELECT COUNT(colName),
(SELECT (column1, column2, column3, column4) AS colName
FROM TableA
WHERE location= location)
AS colCount
FROM TableA
WHERE colName IS NOT NULL
There may be a slicker way but a brute-force way would be: 可能有一种随意的方法,但蛮力的方法是:
SELECT
location,
CASE WHEN column1 IS NULL THEN 0 ELSE 1 END +
CASE WHEN column2 IS NULL THEN 0 ELSE 1 END +
CASE WHEN column3 IS NULL THEN 0 ELSE 1 END +
CASE WHEN column4 IS NULL THEN 0 ELSE 1 END
AS colCount
FROM TableA
select location,
sum(case when column1 is not null then 1 else 0 end) +
sum(case when column2 is not null then 1 else 0 end) +
sum(case when column3 is not null then 1 else 0 end) +
sum(case when column4 is not null then 1 else 0 end) cnt
from TableA
group by location
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.