计算每行中的空列 - SQL

时间:2018-02-01 17:50:09

标签: sql-server

我想计算空列数或=''在SQL的每一行中。并按Row_ID分组。

这样的事情:

SELECT 
    Row_ID, COUNT(*) AS 'cnt_blankCol'
FROM 
    INFORMATION_SCHEMA.COLUMNS
WHERE 
    table_catalog = 'db'
    AND table_name = 'tblName'
    AND COLUMNS IS NULL OR COLUMNS = '' 
GROUP BY 
    Row_ID
ORDER BY 
    COUNT(*)

谢谢。

2 个答案:

答案 0 :(得分:0)

Information_Schema tables are metadata tables that contain information about the database objects them selves, it does not contain the actual data from the tables, and it does not contain data aggregates per object.

This can not be done querying information_schema. Perhaps the Op can update the question and give a scenario of the goal of the question.

答案 1 :(得分:-1)

可根据要求自定义此脚本。

试试这个并告诉我你要找的是什么输出,

declare @tt table(id int identity,col int,col1 varchar(200))
insert into @tt VALUES(null,'gfdgdfg'),(32,'gfdgdfg'),(null,null)
--First make the datatype similar
--id can be rowid or rownumber function
;With CTEDataType as
(
SELECT cast(col as varchar(200))col, cast(col1 as varchar(200))col1,id
FROM @tt 
)
, CTE as
(
SELECT  id,ColName
FROM 
( 
SELECT isnull(col,'')col, isnull(col1,'')col1,id
FROM CTEDataType 
) Main


UNPIVOT 
( 
ColName FOR ColNames IN (col ,col1) 
) t4
)
select id,count(*)from CTE
where colname=''
group by id