从查询中选择列

时间:2014-08-29 09:46:55

标签: sql sql-server tsql analytics

我正在使用请求来获取列名称的集合:

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE [...]

从这个集合中,我想按列名计算原始表组中的每个非空值,而不是空值。

假设我有一个包含

的表格
COL1 | COL2 | COL3
------------------  
VAL1 | VAL2 | NULL
VAL3 |      | VAL4
VAL5 |      | 

我正在寻找要求:

COL1 | 3
COL2 | 1
COL2 | 1

这是出于分析目的。

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

这是一个简单的过程。运行以下查询:

SELECT 'SELECT ''' + COLUMN_NAME + ''', COUNT(['+COLUMN_NAME']) as NotNull FROM [' +SCHEMA_NAME+ '].['+TABLE_NAME+ '] union all '
FROM INFORMATION_SCHEMA.COLUMNS
WHERE [...]

将结果复制到查询窗口中,删除最终的union all,然后运行查询。

答案 1 :(得分:0)

以下代码似乎适用于您的问题

create table sample
(
  col1 varchar(10),
  col2 varchar(10),
  col3 varchar(10)
  )

INSERT INTO sample (COL1,COL2,COL3) VALUES ('VAL1 ',' VAL2 ',NULL);
INSERT INTO sample (COL1,COL2,COL3) VALUES ('VAL3 ','      ',' VAL4');
INSERT INTO sample (COL1,COL2,COL3) VALUES ('VAL5 ','      ',' ');
DECLARE @cols1 NVARCHAR(MAX);
DECLARE @sql NVARCHAR(MAX);

SELECT @cols1 = STUFF((
    SELECT ', COUNT(CASE WHEN len(['+ t1.NAME + '])!=0 THEN 1 END) AS ' + t1.name
    FROM sys.columns AS t1
    WHERE t1.object_id = OBJECT_ID('sample')
    --ORDER BY ', COUNT([' + t1.name + ']) AS ' + t1.name
    FOR XML PATH('')
), 1, 2, '');

SET @sql = '
SELECT ' + @cols1 + '
FROM sample
'




EXEC(@sql)

答案 2 :(得分:0)

这是我对此的一点点看法:

declare @cols table (colID integer, colName varchar(50))
declare @results table (colName nvarchar(50), valueCount bigint)

-- table name
declare @tableName nvarchar(50) = 'INSERT TABLE NAME HERE' 

-- select column names from table
insert into @cols
select column_id, name from sys.columns where object_id = object_id(@tableName) order by column_id

declare @currentColID int = 0
declare @currentName nvarchar(50) = ''
declare @currentCount bigint = 0
declare @sql nvarchar(max) -- where the dynamic sql will be stored

-- go through all columns
while (1 = 1)
begin
    -- step by id
    select top 1 @currentColID = c.colID, @currentName = c.colName from @cols c
    where c.colid > @currentColID order by c.colID

    if @@ROWCOUNT = 0 break;

    -- dynamic query to get non-empty, not-null counts
    select @sql = 'select @p1=COUNT(' + @currentName + ') from ' + @tableName + 
                 ' where ' + @currentName + ' is not null or LEN(' + @currentName + ') > 0'

    exec sp_executesql @sql, N'@p1 bigint output', @p1 = @currentCount output       

    -- insert result to buffer          
    insert into @results values (@currentName, @currentCount)       
end

-- print the buffer
select * from @results

玩得开心:)