T-SQL 如何计算每列的记录数

发布于 2024-11-29 12:54:26 字数 716 浏览 0 评论 0原文

我有一个包含超过 120 列的表,需要确定哪一列使用最少。我尝试使用 sql 查询来执行此操作,但发现 T-SQL 更简单一些。

我尝试了以下操作,但每列的计数结果均为 0。

Declare  data1 Cursor for select column_name 
        from information_schema.columns 
        where table_name = 'repository' 

Declare @mField nvarchar(255)
Declare @count int

Open data1;

fetch next from  data1 into @mField;

set @count = -1;

while @@fetch_status = 0  
    begin
         select   @count = count(@mField)
                    from  repository where tablereference = 
                    'central' and ( @mField!= null )

        print @mField+' ' ; 
                  print @count;
        Fetch next from  data1 into @mField;
    end

close data1;
deallocate data1;

I have a table with over 120 columns and need to determine which column is used the least. I tried using sql queries to do this, but found T-SQL a bit simpler.

I tried the following but my count comes out as 0 for every column.

Declare  data1 Cursor for select column_name 
        from information_schema.columns 
        where table_name = 'repository' 

Declare @mField nvarchar(255)
Declare @count int

Open data1;

fetch next from  data1 into @mField;

set @count = -1;

while @@fetch_status = 0  
    begin
         select   @count = count(@mField)
                    from  repository where tablereference = 
                    'central' and ( @mField!= null )

        print @mField+' ' ; 
                  print @count;
        Fetch next from  data1 into @mField;
    end

close data1;
deallocate data1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玻璃人 2024-12-06 12:54:26

您不能对这样的值进行计数,因为您只是测试 @mField 是否为 NULL。列名称未被替换。

无论如何,COUNT 都会忽略 NULL,因此如果您想计算非空值,请执行以下操作:

DECLARE @sql varchar(4000)

SET @sql = 'SELECT COUNT(*) AS Total '

SELECT @sql = @sql + ', COUNT(' +  QUOTENAME(column_name) + ') AS ' + QUOTENAME(column_name)
from information_schema.columns 
where table_name = 'repository'

SET @sql = @sql + ' FROM repository'

EXEC (@sql)

这会为所有列查询表一次

You can't count values like this because you are only testing if @mField is NULL. The column name isn't substituted.

COUNT ignores NULLs anyway so if you want to count non-null values, do this:

DECLARE @sql varchar(4000)

SET @sql = 'SELECT COUNT(*) AS Total '

SELECT @sql = @sql + ', COUNT(' +  QUOTENAME(column_name) + ') AS ' + QUOTENAME(column_name)
from information_schema.columns 
where table_name = 'repository'

SET @sql = @sql + ' FROM repository'

EXEC (@sql)

This queries the table once for all columns

怎言笑 2024-12-06 12:54:26

您需要在中间使用一些动态sql来实现您的目标。

    Declare  data1 Cursor for select column_name 
            from information_schema.columns 
            where table_name = 'repository' 

    Declare @mField nvarchar(255)


    Open data1;

    fetch next from  data1 into @mField;


    while @@fetch_status = 0  
    begin
        exec ('

            declare @count int

            select @count = count([' + @mField + '])
                        from  repository  where tablereference = 
                    ''central'' and ( [' + @mField + '] is not null)                                    

            if @count < 10
            begin
                print ''' + @mField + ' '' ; 
                print @count;
            end         
        ')
        Fetch next from  data1 into @mField;
    end

close data1;
deallocate data1;

You need to use some dynamic sql in the middle to acheive your aim here.

    Declare  data1 Cursor for select column_name 
            from information_schema.columns 
            where table_name = 'repository' 

    Declare @mField nvarchar(255)


    Open data1;

    fetch next from  data1 into @mField;


    while @@fetch_status = 0  
    begin
        exec ('

            declare @count int

            select @count = count([' + @mField + '])
                        from  repository  where tablereference = 
                    ''central'' and ( [' + @mField + '] is not null)                                    

            if @count < 10
            begin
                print ''' + @mField + ' '' ; 
                print @count;
            end         
        ')
        Fetch next from  data1 into @mField;
    end

close data1;
deallocate data1;
扭转时空 2024-12-06 12:54:26

您的 count(@mField)@mField 中恰好出现的文字值的计数,它不会将字段名称解析为 < code>COUNT(fldBlah),您需要为此使用动态 SQL。

Declare data1 Cursor FAST_FORWARD for select column_name 
        from information_schema.columns where table_name = 'repository' 

Declare @mField nvarchar(255)
Declare @SQL varchar(1024)
Declare @results table (col_name varchar(128), non_nulls int)

Open data1;
fetch next from data1 into @mField;

while (@@FETCH_STATUS = 0) begin
    set @SQL = 'SELECT ''' + @mField + ''', count(' + @mField + ') from repository where tablereference =  ''central'''
    insert @results
               exec(@SQL)
    Fetch next from data1 into @mField;
end

close data1;
deallocate data1;

select * from @results

对于类似的输出;

col_name            non_nulls
[tablereference]    5
[another_col]       1

tablereference 列中有 5 个非空值等

Your count(@mField) is a count of the litteral value that happens to be in @mField, its not resolving the field name into COUNT(fldBlah), you would need to use dynamic SQL for that.

Declare data1 Cursor FAST_FORWARD for select column_name 
        from information_schema.columns where table_name = 'repository' 

Declare @mField nvarchar(255)
Declare @SQL varchar(1024)
Declare @results table (col_name varchar(128), non_nulls int)

Open data1;
fetch next from data1 into @mField;

while (@@FETCH_STATUS = 0) begin
    set @SQL = 'SELECT ''' + @mField + ''', count(' + @mField + ') from repository where tablereference =  ''central'''
    insert @results
               exec(@SQL)
    Fetch next from data1 into @mField;
end

close data1;
deallocate data1;

select * from @results

For an output like;

col_name            non_nulls
[tablereference]    5
[another_col]       1

Where there are 5 non-null values in column tablereference etc

萧瑟寒风 2024-12-06 12:54:26

您需要使用 IS NOT NULL 而不是 != NULL

You need to use IS NOT NULL instead of != NULL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文