Mysql

关注公众号 jb51net

关闭
首页 > 数据库 > Mysql > Mysql使用Union行合并

Mysql中使用Union—多表合并之行合并

作者:榴莲小冰棍

这篇文章主要介绍了Mysql中使用Union—多表合并之行合并,如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字,union(或称为联合)的作用是将多个结果合并在一起显示出来,需要的朋友可以参考下

Union (all)语句格式

select 列名    from 表A

union (all)

select 列名    from 表B

注意事项:

案例解析

统计20170703—20170709周内每天及本周累计销售金额、订单量、会员数、订单占比

  • 统计时间段内每天的累计销售金额、订单量、会员数
  • 统计本周累计销售金额、订单量、会员数
  • 订单占比
  • union合并表
  • 对比 union all合并表——因为没有重复数据,所以两表合并结果一样
-- 1、统计时间段内每天的累计销售金额、订单量、会员数
SELECT DATE_FORMAT(dimDateID,'%W') as week_1      /*把时间转化为星期模式*/
      ,SUM(AMT) as money
      ,COUNT(DISTINCT salesID) as num_order       /*一个会员一周可以下多单,所以要统计会员数,需要去重*/
      ,COUNT(DISTINCT dimMemberID) num_member
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W') ;
-- 2、统计本周累计销售金额、订单量、会员数
SELECT SUM(AMT) as total_money
      ,COUNT(DISTINCT salesID) as total_num_order
      ,COUNT(DISTINCT dimMemberID) total_num_member
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0;
-- 3、订单占比
SELECT DATE_FORMAT(dimDateID,'%W') AS week_1
      ,CONCAT(ROUND( COUNT(DISTINCT salesID)/(SELECT COUNT(DISTINCT salesID)
                                FROM dw.fct_sales
                                where dimDateID between '20170703' and '20170709'
                                and dimMemberID <>0),4)*100,'%') as order_rate
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W') ;
-- 4、union合并表
SELECT DATE_FORMAT(dimDateID,'%W') AS week_1 
      ,SUM(AMT) as money
      ,COUNT(DISTINCT salesID) as num_order
      ,COUNT(DISTINCT dimMemberID) num_member
      ,CONCAT(ROUND( COUNT(DISTINCT salesID)/(
       SELECT COUNT(DISTINCT salesID)
       FROM dw.fct_sales
       where dimDateID between '20170703' and '20170709'
       and dimMemberID <>0),4)*100,'%') as order_rate
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W')
UNION 
SELECT week(dimDateID,1)               /*为了保证列数一样,week返回日期为一年中的第几周 weel(date,1):从周一开始为第一天*/
      ,SUM(AMT) as total_money
      ,COUNT(DISTINCT salesID) as total_num_order
      ,COUNT(DISTINCT dimMemberID) total_num_member
      ,'100%' as total                 /*为确保列数一样*/
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by week(dimDateID,1);            /*出现汇总函数需要进行分组*/
-- 5、对比 union all合并表——因为没有重复数据,所以两表合并结果一样
SELECT DATE_FORMAT(dimDateID,'%W') AS week_1 
      ,SUM(AMT) as money
      ,COUNT(DISTINCT salesID) as num_order
      ,COUNT(DISTINCT dimMemberID) num_member
      ,CONCAT(ROUND( COUNT(DISTINCT salesID)/(
       SELECT COUNT(DISTINCT salesID)
       FROM dw.fct_sales
       where dimDateID between '20170703' and '20170709'
       and dimMemberID <>0),4)*100,'%') as order_rate
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W')
UNION ALL 
SELECT week(dimDateID,1)               /*为了保证列数一样,week返回日期为一年中的第几周 weel(date,1):从周一开始为第一天*/
      ,SUM(AMT) as total_money
      ,COUNT(DISTINCT salesID) as total_num_order
      ,COUNT(DISTINCT dimMemberID) total_num_member
      ,'100%' as total                 /*为确保列数一样*/
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by week(dimDateID,1);            /*出现汇总函数需要进行分组*/
区分union 和 union all ,利用重复数据对比,合并两个一模一样的表 
-- union
SELECT DATE_FORMAT(dimDateID,'%W') as week_1      /*把时间转化为星期模式*/
      ,SUM(AMT) as money
      ,COUNT(DISTINCT salesID) as num_order       /*一个会员一周可以下多单,所以要统计会员数,需要去重*/
      ,COUNT(DISTINCT dimMemberID) num_member
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W')
UNION 
SELECT DATE_FORMAT(dimDateID,'%W') as week_1      /*把时间转化为星期模式*/
      ,SUM(AMT) as money
      ,COUNT(DISTINCT salesID) as num_order       /*一个会员一周可以下多单,所以要统计会员数,需要去重*/
      ,COUNT(DISTINCT dimMemberID) num_member
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W') ;
-- union all
SELECT DATE_FORMAT(dimDateID,'%W') as week_1      /*把时间转化为星期模式*/
      ,SUM(AMT) as money
      ,COUNT(DISTINCT salesID) as num_order       /*一个会员一周可以下多单,所以要统计会员数,需要去重*/
      ,COUNT(DISTINCT dimMemberID) num_member
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W')
UNION ALL 
SELECT DATE_FORMAT(dimDateID,'%W') as week_1      /*把时间转化为星期模式*/
      ,SUM(AMT) as money
      ,COUNT(DISTINCT salesID) as num_order       /*一个会员一周可以下多单,所以要统计会员数,需要去重*/
      ,COUNT(DISTINCT dimMemberID) num_member
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W') ;
多表合并中的排序问题 order by——把合并后的表作为一个临时表,再进行排序

注意临时表需要命名

排序要对新的列名进行排序

SELECT sn.*
FROM (
      SELECT DATE_FORMAT(dimDateID,'%W') as week_1      /*把时间转化为星期模式*/
            ,SUM(AMT) as money 
            ,COUNT(DISTINCT salesID) as num_order       /*一个会员一周可以下多单,所以要统计会员数,需要去重*/
            ,COUNT(DISTINCT dimMemberID) num_member
      FROM dw.fct_sales
      where dimDateID between '20170703' and '20170709'
      and dimMemberID <>0
      group by DATE_FORMAT(dimDateID,'%W')
      UNION 
      SELECT DATE_FORMAT(dimDateID,'%W') as week_1      /*把时间转化为星期模式*/
            ,SUM(AMT) as money 
            ,COUNT(DISTINCT salesID) as num_order       /*一个会员一周可以下多单,所以要统计会员数,需要去重*/
            ,COUNT(DISTINCT dimMemberID) num_member
      FROM dw.fct_sales
      where dimDateID between '20170703' and '20170709'
      and dimMemberID <>0
      group by DATE_FORMAT(dimDateID,'%W') ) as sn
order by money desc;                                    /*注意要对money排序,而不是SUM(AMT)*/
-- order by SUM(AMT) desc;                              /*会报错,需要用临时表的列名,并且要对临时表取列名,否则也会报错*/

到此这篇关于Mysql中使用Union—多表合并之行合并的文章就介绍到这了,更多相关Mysql使用Union行合并内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文