获取 Teradata 表中每列的缺失值和非缺失值的计数

发布于 2025-01-15 15:01:49 字数 827 浏览 6 评论 0原文

我在 Teradata 中有一个包含以下类似数据的表,我想计算表中每列的总缺失记录和总非缺失记录。

输入图片此处描述

预期结果:

在此处输入图像描述

目前我正在使用以下查询

SELECT 'New_York' Column_Name
, SUM(CASE WHEN New_York <> ''  THEN 1 ELSE 0 END) Nonmissing
,SUM(CASE WHEN New_York = '' THEN 1 ELSE 0 END) Missing
from Table A

SELECT 'London' Column_Name
, SUM(CASE WHEN London <> ''  THEN 1 ELSE 0 END) Nonmissing
,SUM(CASE WHEN London = '' THEN 1 ELSE 0 END) Missing
from Table A

任何人都可以提供建议吗?循环遍历所有列以创建单个查询,而不是使用不同的列名多次重复相同的查询。我的表中有大约 430 列,多次重复相同的查询是不可行的。任何建议都将受到高度赞赏。

I have a table with below similar data in Teradata and I want to count the total missing and total non missing records of each column in the table.

enter image description here

Expected result:

enter image description here

Currently I'm using below query

SELECT 'New_York' Column_Name
, SUM(CASE WHEN New_York <> ''  THEN 1 ELSE 0 END) Nonmissing
,SUM(CASE WHEN New_York = '' THEN 1 ELSE 0 END) Missing
from Table A

SELECT 'London' Column_Name
, SUM(CASE WHEN London <> ''  THEN 1 ELSE 0 END) Nonmissing
,SUM(CASE WHEN London = '' THEN 1 ELSE 0 END) Missing
from Table A

and so on

Can anyone please advice if there is anyway we can loop through all columns to create single query instead of repeating the same query multiple time with different column names. I have around 430 columns in the table and repeating same query multiple time is not feasible. Any advice is highly appreciated.

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

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

发布评论

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

评论(1

怀念你的温柔 2025-01-22 15:01:49

试试这个。它非常通用的 SQL,它应该适合你。

declare @sql nvarchar(max)
declare @allsql nvarchar(max) = ''

declare db_cursor cursor for
select   
    'SELECT ''' + column_name + ''' AS CityName, (select count(*) from CityNumbers where ' + column_name + ' IS NULL) AS Missing, (select count(*) from CityNumbers where ' + column_name + ' IS NOT NULL) AS NonMissing UNION ' AS sql
from
    INFORMATION_SCHEMA.COLUMNS
    where TABLE_NAME = 'CityNumbers'

open db_cursor
fetch next from db_cursor into @sql

while @@FETCH_STATUS=0
begin
    set @allsql = @allsql + @sql
    fetch next from db_cursor into @sql
end
close db_cursor
deallocate db_cursor

select @allsql = left(@allsql,len(@allsql)-6)
select @allsql

exec (@allsql)

结果如下:

CityNameMissingNonMissing
Dubai32
London23
NewYork14
Paris05
Singapore14

Try this. Its pretty generic SQL, it should work for you.

declare @sql nvarchar(max)
declare @allsql nvarchar(max) = ''

declare db_cursor cursor for
select   
    'SELECT ''' + column_name + ''' AS CityName, (select count(*) from CityNumbers where ' + column_name + ' IS NULL) AS Missing, (select count(*) from CityNumbers where ' + column_name + ' IS NOT NULL) AS NonMissing UNION ' AS sql
from
    INFORMATION_SCHEMA.COLUMNS
    where TABLE_NAME = 'CityNumbers'

open db_cursor
fetch next from db_cursor into @sql

while @@FETCH_STATUS=0
begin
    set @allsql = @allsql + @sql
    fetch next from db_cursor into @sql
end
close db_cursor
deallocate db_cursor

select @allsql = left(@allsql,len(@allsql)-6)
select @allsql

exec (@allsql)

Here's the result:

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