查询以查找 ID 总数和在任何列中具有 null 的 ID 以及两者的百分比(即具有 null 的 ID 计数/ID 总数)
Query to find the count of total IDs, and IDs having null in any column and the percentage of the two (i.e. Count of ID having null /total ID count)
有两张桌子。
Tables A 具有以下结构
ID
Flag
Name
1X
1
Y
2Y
0
Null
3Z
1
Null
4A
1
Y
Table B 有如下结构
B_ID
City
State
1X
Y
Null
2Y
Null
Null
3Z
Null
Y
4A
Y
Y
我想获取所有 ID 的计数以及在任何列(名称、城市、州)中具有 Null 的 ID 的计数,例如,从上表中只有 ID 4A 具有非空值在两个表的所有三列中,所以输出应该像
Total_Count
Ids having null
Percentage missing
4
3
0.75%
Total_count 是 4,因为总共有四个 ID,具有 NULL 的 ID 是 3,因为在三列(即名称、城市、州)中的任何一个中都有 3 个 ID 为空,并且缺失百分比只是具有空值的 ID / Total_Count.
我尝试使用以下几行查询
select (count/total) * 100 pct, count,total
from (select sum(count) count
from(select count(*) count from tableA T1
where T1.name is null
union all
select count(*) count from tableA T1
join tableB T2 on T1.ID = T2.B_ID
where T2.city is null
union all
select count(*) count from tableA T1
join tableB T2 on T1.ID = T2.B_ID
where T2.state is null)),
select count(ID) total from tableA);
但是查询没有返回所需的输出,你能给我一个更好的方法吗?
谢谢
使用条件聚合:
SELECT COUNT(*) Total_Count,
COUNT(CASE WHEN t1.Name IS NULL OR t2.City IS NULL OR t2.State IS NULL THEN 1 END) Ids_having_null,
AVG(CASE WHEN COALESCE(t1.Name, t2.City, t2.State) IS NOT NULL THEN 1 ELSE 0 END) Percentage_missing
FROM Table1 t1 INNER JOIN Table2 t2
ON t2.B_ID = t1.ID;
参见demo。
如果您不知道 table 是否拥有所有 ID?
然后我建议 full join
在子查询中使用一些条件聚合。
例如:
select
Total as "Total_Count"
, TotalMissing as "Ids having null"
, (TotalMissing / Total)*100||'%' as "Percentage missing"
from
(
select
count(distinct coalesce(a.ID, b.B_ID)) as Total
, count(distinct case
when a.name is null
or b.city is null
or b.state is null
then coalesce(a.ID, b.B_ID)
end) as TotalMissing
from TableA a
full outer join TableB b
on a.ID = b.B_ID
) q
Total_Count | Ids having null | Percentage missing
----------: | --------------: | :-----------------
4 | 3 | 75%
db<>fiddle here
试试这个 ->
select total_count, (total_count - cn) as ids_having_null,
(total_count - cn) *100 / total_count as Percentage_missing
FROM
(select count(t1.ID) as cn , t1.total_count
FROM ( select ID,Name, sum(tmp_col) over ( order by tmp_col) as total_count from (select ID,Name, 1 as tmp_col from tableA ) ) t1
JOIN TableB t2
ON t1.ID = t2.B_ID
WHERE t1.Name is not null and t2.City is not null and t2.State is not null );
根据您对百分比或比率的要求,您可以更改 Percentage_missing 列的逻辑
有两张桌子。 Tables A 具有以下结构
ID | Flag | Name |
---|---|---|
1X | 1 | Y |
2Y | 0 | Null |
3Z | 1 | Null |
4A | 1 | Y |
Table B 有如下结构
B_ID | City | State |
---|---|---|
1X | Y | Null |
2Y | Null | Null |
3Z | Null | Y |
4A | Y | Y |
我想获取所有 ID 的计数以及在任何列(名称、城市、州)中具有 Null 的 ID 的计数,例如,从上表中只有 ID 4A 具有非空值在两个表的所有三列中,所以输出应该像
Total_Count | Ids having null | Percentage missing |
---|---|---|
4 | 3 | 0.75% |
Total_count 是 4,因为总共有四个 ID,具有 NULL 的 ID 是 3,因为在三列(即名称、城市、州)中的任何一个中都有 3 个 ID 为空,并且缺失百分比只是具有空值的 ID / Total_Count.
我尝试使用以下几行查询
select (count/total) * 100 pct, count,total
from (select sum(count) count
from(select count(*) count from tableA T1
where T1.name is null
union all
select count(*) count from tableA T1
join tableB T2 on T1.ID = T2.B_ID
where T2.city is null
union all
select count(*) count from tableA T1
join tableB T2 on T1.ID = T2.B_ID
where T2.state is null)),
select count(ID) total from tableA);
但是查询没有返回所需的输出,你能给我一个更好的方法吗? 谢谢
使用条件聚合:
SELECT COUNT(*) Total_Count,
COUNT(CASE WHEN t1.Name IS NULL OR t2.City IS NULL OR t2.State IS NULL THEN 1 END) Ids_having_null,
AVG(CASE WHEN COALESCE(t1.Name, t2.City, t2.State) IS NOT NULL THEN 1 ELSE 0 END) Percentage_missing
FROM Table1 t1 INNER JOIN Table2 t2
ON t2.B_ID = t1.ID;
参见demo。
如果您不知道 table 是否拥有所有 ID?
然后我建议 full join
在子查询中使用一些条件聚合。
例如:
select Total as "Total_Count" , TotalMissing as "Ids having null" , (TotalMissing / Total)*100||'%' as "Percentage missing" from ( select count(distinct coalesce(a.ID, b.B_ID)) as Total , count(distinct case when a.name is null or b.city is null or b.state is null then coalesce(a.ID, b.B_ID) end) as TotalMissing from TableA a full outer join TableB b on a.ID = b.B_ID ) q
Total_Count | Ids having null | Percentage missing ----------: | --------------: | :----------------- 4 | 3 | 75%
db<>fiddle here
试试这个 ->
select total_count, (total_count - cn) as ids_having_null,
(total_count - cn) *100 / total_count as Percentage_missing
FROM
(select count(t1.ID) as cn , t1.total_count
FROM ( select ID,Name, sum(tmp_col) over ( order by tmp_col) as total_count from (select ID,Name, 1 as tmp_col from tableA ) ) t1
JOIN TableB t2
ON t1.ID = t2.B_ID
WHERE t1.Name is not null and t2.City is not null and t2.State is not null );
根据您对百分比或比率的要求,您可以更改 Percentage_missing 列的逻辑