正文
(orderid)
as
sales
from
t
where
deal_date
between
"2019-05-01"
and
"2019-05-31"
group
by
area
,province
城市成交量
select
area
,province
,city
,count(orderid) as sales
from
t
where
deal_date between "2019-05-01" and "2019-05-31"
group by
area
,province
,city
店铺成交量
select
area
,province
,city
,shop
,count(orderid) as sales
from
t
where
deal_date between "2019-05-01" and "2019-05-31"
group by
area
,province
,city
,shop
上面这种方法可以达到我们的目的,满足我们的需求,但是这种方法太低效了,我们在Excel中还需要做合并处理,很麻烦。
能不能把上面几种结果在 Sql 中就进行合并处理,这样就不需要在 Excel 中合并了。
答案是可以的,需要借助的就是 union和 union all,对查询结果进行纵向合并。
union 和 union all的区别在于:
前者是对合并后的结果进行去重处理,而后者返回合并后的所有数据。
具体代码如下:
select null,null,null,null,count(orderid) as sales from t where deal_date between "2019-05-01" and "2019-05-31"
union all
select area,null,null,null,count(orderid) as sales from t where deal_date between "2019-05-01" and "2019-05-31"group by area
union all