大数据 爬虫Python AI SqlSQL极简教程 · MySQL · MyBatis · JPA 技术笔记 教程 总结SQL server

SQL查询使用指南之去重

2020-03-04  本文已影响0人  北极熊在冬眠
  1. 数据示例


    table_purchase.png
  2. 数据解释

  1. 数据要求

4.分析
去重的常规做法是【 group by +分类项】&【 distinct +去重项】,而SQL是先运行 group by 再运行 select 中的内容,因此第二种要求则不适用这种做法:如A顾客在1/1号购买了一件商品,在1/2也购买了一件商品,如使用常规方式,则在每日增长的统计中就被计算了2次。因此需要先去重,再group by。

  1. SQL语句


    JULIA大神的SQL运行顺序图(来自微博博主@爱可可-爱生活).jpeg

    -各个商品类型的购买人数

select p.sku_type, count(distinct p.clientid) as 'client_amount'
from purchase as p
group by p.sku_type

1.group by p.sku_type

group by p.sku_type.png
2.select p.sku_type, count(distinct p.clientid) as 'client_amount'
select p.sku_type, count(distinct p.clientid) as 'client_amount'.png
select a.date_format(date(a.b),'%Y-%m-%d') as 'purchase_time'
, count(distinct a.clientid) as 'new_client_amount'
from 
(select p.clientid, date_format(MIN(date(p.purchase_time)),'%Y-%m-%d') as b
from purchase as p
group by p.clientid) as a
group by date(a.b)
order by date(a.b) ASC
from 
(select p.clientid, date_format(MIN(date(p.purchase_time)),'%Y-%m-%d') as b
from purchase as p
group by p.clientid) as a

1). group by p.clientid

group by p.clientid.png
2). select p.clientid, date_format(MIN(date(p.purchase_time)),'%Y-%m-%d') as b' select p.clientid, date_format(MIN(date(p.purchase_time)),'%Y-%m-%d') as b.png
  1. group by date(a.b)

    group by date(a.b).png
select a.date_format(date(a.b),'%Y-%m-%d') as 'purchase_time', 
count(distinct a.clientid) as 'new_client_amount' 
select a.date_format(date(a.b),'%Y-%m-%d') as 'purchase_time', count(distinct a.clientid) as 'new_client_amount' .png
上一篇 下一篇

猜你喜欢

热点阅读