【问题标题】:Merging different SQL scripts into one script将不同的 SQL 脚本合并到一个脚本中
【发布时间】:2017-06-06 14:20:13
【问题描述】:

我有 3 个查询脚本在同一个表上工作;一个是计算总行数,另一个是计算重复行的总数,另一个是计算不同(唯一)行的总数。

统计总行数:

select count(*) as total_rows from <table_name>

计算重复行数:

select COUNT (<column_name>) as total_duplicate from <table_name> group by (<column_name>) HAVING count(<column_name>) > 1

计算唯一行数:

select COUNT(DISTINCT <column_name>) as total_unique from <table_name>

我正在尝试将所有 3 个合并到一个脚本中。我能够为总计数和唯一计数做到这一点;但是,我不确定如何包含重复计数查询,因为它有一个 group by 语句。

请有什么想法。

【问题讨论】:

    标签: sql hive


    【解决方案1】:

    尝试这样做:

    select count(*) as total_rows,COUNT(DISTINCT <column_name>) as total_unique, 
        (select COUNT (<column_name>) 
         from <table_name> 
         group by (<column_name>) 
         HAVING count(<column_name>) > 1) as total_duplicate
    from <table_name>
    

    【讨论】:

    • 如所写,您将获得“子查询返回超过 1 个值”。有多个重复项时出错。
    • @BaconBits - 不,甚至没有。至少目前,Hive 不支持 SELECT 子句中的子查询
    【解决方案2】:
    select      sum(rows_per_col)   as total_rows
               ,count (is_dup)      as total_duplicate
               ,count(*)            as total_unique
    
    from       (select      column_name
                           ,case when count(*) > 1 then 1 end   as is_dup
                           ,count(*)                            as rows_per_col
                
                from        table_name
                
                group by    column_name
                ) t
    

    演示

    with table_name as (select explode(array(1,1,1,2,3,3,4,4,4,4,4,5)) as column_name)
    
    select      sum(rows_per_col)   as total_rows
               ,count (is_dup)      as total_duplicate
               ,count(*)            as total_unique
    
    from       (select      column_name
                           ,case when count(*) > 1 then 1 end   as is_dup
                           ,count(*)                            as rows_per_col
    
                from        table_name
    
                group by    column_name
                ) t
               
    

    +------------+-----------------+--------------+
    | total_rows | total_duplicate | total_unique |
    +------------+-----------------+--------------+
    |         12 |               3 |            5 |
    +------------+-----------------+--------------+
    

    【讨论】:

      【解决方案3】:

      您可以将这些合并在一起:

       select count(*) as count_total
              'total_rows' as count_type
       from <table_name>
      
       union
      
       select count (<column_name>)as count_total
              'total_duplicate' as count_type
       from <table_name>
       group by (<column_name>)
       HAVING count(<column_name>) > 1
      
       union
      
       select COUNT(DISTINCT <column_name>) as count_total
              'total_unique' as count type
       from <table_name>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-04
        • 2014-11-06
        • 1970-01-01
        • 1970-01-01
        • 2017-11-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多