• grouping sets从属子句的运用


    grouping sets主要是用来合并多个分组的结果。

    对于员工目标业绩表‘businessTarget’:

    employeeId    targetDate    idealDistAmount
    10098              2016-05                    100000
    10099              2016-05                    80000
    10100              2016-05                    80000
    10101              2016-05                    100000
    10102              2016-05                    50000
    10103              2016-05                    50000
    10104              2016-05                    50000
    10118              2016-05                    50000
    10130             2016-05                    507689
    10091             2016-07                    20000
    10290             2016-08                    20000
    10291             2016-08                    20000
    9058             2016-08                    10000
    9792             2016-07                    20000
    9865             2016-07                    20000

    如果需要分别对上表employeeId,targetDate,(employeeId,targetDate)分别进行group by,代码如下:

     1 select  employeeId,null,sum(idealDistAmount)
     2 from businessTarget
     3 group by employeeId
     4 union all
     5 select  null,targetDate,sum(idealDistAmount)
     6 from businessTarget
     7 group by targetDate
     8 union all
     9 select  employeeId,targetDate,sum(idealDistAmount)
    10 from businessTarget
    11 group by employeeId,targetDate

    结果:

     1 employeeId    (无列名)    (无列名)
     2 9058    NULL    10000
     3 9792    NULL    20000
     4 9865    NULL    20000
     5 10091    NULL    20000
     6 10098    NULL    100000
     7 10099    NULL    80000
     8 10100    NULL    80000
     9 10101    NULL    100000
    10 10102    NULL    50000
    11 10103    NULL    50000
    12 10104    NULL    50000
    13 10118    NULL    50000
    14 10130    NULL    507689
    15 10290    NULL    20000
    16 10291    NULL    20000
    17 NULL    2016-05    1067689
    18 NULL    2016-07    60000
    19 NULL    2016-08    50000
    20 10098    2016-05    100000
    21 10099    2016-05    80000
    22 10100    2016-05    80000
    23 10101    2016-05    100000
    24 10102    2016-05    50000
    25 10103    2016-05    50000
    26 10104    2016-05    50000
    27 10118    2016-05    50000
    28 10130    2016-05    507689
    29 9792    2016-07    20000
    30 9865    2016-07    20000
    31 10091    2016-07    20000
    32 9058    2016-08    10000
    33 10290    2016-08    20000
    34 10291    2016-08    20000
    View Code

    如果我们运用grouping sets来指定多个group by 选项,

    就可以通过一条select 语句实现复杂繁琐的多条select 语句的查询,并且更加的高效。
    1 select  employeeId,targetDate,sum(idealDistAmount)
    2 from businessTarget
    3 group by
    4  grouping sets
    5  (
    6  (employeeId),
    7  (targetDate),
    8  (employeeId,targetDate),()
    9  )

    结果:

     1 employeeId    targetDate    (无列名)
     2 10098    2016-05    100000
     3 10099    2016-05    80000
     4 10100    2016-05    80000
     5 10101    2016-05    100000
     6 10102    2016-05    50000
     7 10103    2016-05    50000
     8 10104    2016-05    50000
     9 10118    2016-05    50000
    10 10130    2016-05    507689
    11 NULL    2016-05    1067689
    12 9792    2016-07    20000
    13 9865    2016-07    20000
    14 10091    2016-07    20000
    15 NULL    2016-07    60000
    16 9058    2016-08    10000
    17 10290    2016-08    20000
    18 10291    2016-08    20000
    19 NULL    2016-08    50000
    20 NULL    NULL    1177689
    21 9058    NULL    10000
    22 9792    NULL    20000
    23 9865    NULL    20000
    24 10091    NULL    20000
    25 10098    NULL    100000
    26 10099    NULL    80000
    27 10100    NULL    80000
    28 10101    NULL    100000
    29 10102    NULL    50000
    30 10103    NULL    50000
    31 10104    NULL    50000
    32 10118    NULL    50000
    33 10130    NULL    507689
    34 10290    NULL    20000
    35 10291    NULL    20000
    View Code

    除了grouping sets从属子句,我们还可以运用另外一个从属子句同样可以得到这样的结果,

    那就是cube从属子句,代码如下:

    1 select  employeeId,targetDate,sum(idealDistAmount)
    2 from businessTarget
    3 group by
    4 cube(employeeId,targetDate)--等同于grouping sets((employeeId),(targetDate),(employeeId,targetDate),())
    5 order by employeeId

    此三种方法运行得到的结果是一样的。

  • 相关阅读:
    python scapy的用法之ARP主机扫描和ARP欺骗
    Python字典取键、值对
    python字典添加元素和删除元素
    Python删除列表元素
    获取本机的IP地址和mac地址
    Python查找电话号码归属地、邮编、运营商信息等
    Python的字符串格式化,%与format
    Python基础笔记一之字符转化、复数、位运算、除法运算、floor和ceil取整,round函数四舍五入
    FAILED: SemanticException Unable to determine if hdfs://tmaster:8020/user/root/words.db/test_t2 is encrypted
    Pyspark读取csv文件
  • 原文地址:https://www.cnblogs.com/kevin-kw/p/6150117.html
Copyright © 2020-2023  润新知