【问题标题】: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)查询以查找总 ID 的计数,以及在任何列中具有 null 的 ID 以及两者的百分比(即具有 null 的 ID 计数/总 ID 计数)
【发布时间】:2021-12-31 12:00:02
【问题描述】:

有两张桌子。 表A具有以下结构

ID Flag Name
1X 1 Y
2Y 0 Null
3Z 1 Null
4A 1 Y

表 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 具有null / 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);

但是查询没有返回所需的输出,您能建议我一个更好的方法吗? 谢谢你

【问题讨论】:

    标签: sql oracle count average


    【解决方案1】:

    试试这个 ->

    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 列的逻辑

    【讨论】:

      【解决方案2】:

      如果您不知道任一表是否包含所有 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 |具有 null | 的 ID缺失百分比 ----------: | --------------: | :----------------- 4 | 3 | 75%

      db小提琴here

      【讨论】:

        【解决方案3】:

        使用条件聚合:

        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

        【讨论】:

        • 感谢您的建议,非常感谢! @forpas
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-17
        • 1970-01-01
        • 2014-11-09
        • 2020-10-08
        • 2021-05-14
        • 2022-12-05
        • 2015-10-28
        相关资源
        最近更新 更多