网上看的一个SQL需求写的SQL(有点难度哦)

需求如下:
Customer_List    --会员资料表
Customerkey      --会员编码 varchar2 不重复
storeid          --会员所属门店编码 varchar2

customer_consumption --会员消费表 
Customerkey   --会员编码 varchar2
storeid       --会员消费门店编码 varchar2
Createdate    --会员消费日期 Date 

需求:
会员归属门店,默认以第一次消费门店为会员归属门店 
后续会员在最近三个月内连续就餐次数最高的门店,自动设为会员资料的归属门店
如果最高就餐次数不够4次(含4次) 以上的 ,不予以处理

注意点:
1. 最近三个月内连续就餐次数最高

2. 最高就餐次数不够4次(含4次) 以上的 ,不予以处理


分许:1 把最近三个月的消费记录完全取出来 ( 这边还要确定下   比如 5月2 号的记录   这里是指 3  4  5 ? 还要  2月2号到5月2号 )?

               (题外话  如果是消费记录的 前三个月,那难度又添加了,如:现在 系统时间是 9月3日, 单现在要录入 8月3号的数据, 前3月就是 6 月, 7 月, 还有8 月)

           2  明显的就是取出最值, 再在最值上面过滤 。。。


分析到这两点 差不多了,

1  制作数据:

     with  tab1 as(
 select 'customer001' Customerkey , 'story002' storeid , to_date('2015-04-21 12:29:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
 union all
 select 'customer001' Customerkey , 'story002' storeid , to_date('2015-05-21 17:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
  union all
 select 'customer001' Customerkey , 'story002' storeid , to_date('2015-06-11 19:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
 union all
  select 'customer001' Customerkey , 'story001' storeid , to_date('2015-07-21 12:29:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
 union all
 select 'customer001' Customerkey , 'story002' storeid , to_date('2015-04-28 17:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
  union all
 select 'customer001' Customerkey , 'story002' storeid , to_date('2015-07-01 19:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
 union all
  select 'customer002' Customerkey , 'story001' storeid , to_date('2015-04-21 12:29:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
 union all
 select 'customer002' Customerkey , 'story002' storeid , to_date('2015-04-21 17:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
  union all
 select 'customer002' Customerkey , 'story002' storeid , to_date('2015-04-11 19:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
  union all
  select 'customer002' Customerkey , 'story001' storeid , to_date('2015-04-06 12:29:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
 union all
 select 'customer002' Customerkey , 'story002' storeid , to_date('2015-04-03 17:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
  union all
 select 'customer002' Customerkey , 'story002' storeid , to_date('2015-08-04 19:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
)

2 统计客户在店中的消费记录

   tab2 as (
 select  customerkey, storeid,  count(*) cnt   from  tab1 group by Customerkey,storeid 
 )  

3  获取最值 

  tab2 as (
 select  customerkey, storeid,  count(*) cnt , max( count(*) ) over(partition by customerkey)  mx  from  tab1 group by Customerkey,storeid 
 ) select tab2.* from tab2;

 注意:  max( count(*) ) over(partition by customerkey) mx  的意思是: 客户在哪个店中的消费记录最多。 

  截图: 


这个表面 001 用户在  002店中 消费到5次... 符合要求。

最后的SQL  :

 
with  tab1 as(
 select 'customer001' Customerkey , 'story002' storeid , to_date('2015-04-21 12:29:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
 union all
 select 'customer001' Customerkey , 'story002' storeid , to_date('2015-05-21 17:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
  union all
 select 'customer001' Customerkey , 'story002' storeid , to_date('2015-06-11 19:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
 union all
  select 'customer001' Customerkey , 'story001' storeid , to_date('2015-07-21 12:29:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
 union all
 select 'customer001' Customerkey , 'story002' storeid , to_date('2015-04-28 17:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
  union all
 select 'customer001' Customerkey , 'story002' storeid , to_date('2015-07-01 19:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
 union all
  select 'customer002' Customerkey , 'story001' storeid , to_date('2015-04-21 12:29:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
 union all
 select 'customer002' Customerkey , 'story002' storeid , to_date('2015-04-21 17:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
  union all
 select 'customer002' Customerkey , 'story002' storeid , to_date('2015-04-11 19:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
  union all
  select 'customer002' Customerkey , 'story001' storeid , to_date('2015-04-06 12:29:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
 union all
 select 'customer002' Customerkey , 'story002' storeid , to_date('2015-04-03 17:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
  union all
 select 'customer002' Customerkey , 'story002' storeid , to_date('2015-08-04 19:00:19','yyyy-mm-dd hh24:mi:ss') Createdate from dual
),
 tab2 as (
 select  customerkey, storeid,  count(*) cnt , max( count(*) ) over(partition by customerkey) mx  from  tab1 group by Customerkey,storeid 
  having count(*)>4
 ) select tab2.* from tab2 where cnt = mx ;

 

说明: tab1  中全是 满足最近3个月的数据, 加入到项目中 自己在表中刷选即可, 另外这个题目要求是更改 会员的归属店, 这个可以过程在数据库中跑, 根据那个SQL 中店编号, 核对会员的归属店编号, 改动.... 这个这里不写了就,  也可以用merger into 来改, merger into 全表比对, 根据实际的情况取舍。 个人倾向于第一个过程。 

最后的结果是: 


一、简介 1.1前言 1、由于最近工作一直用Oracle,故对Oracle数据库研究为对象。 2、根据工作业务需求实际情况进行功能研发。为什么要开发呢?因为在数据库升级或者迁移的时候,为了保证不同环境不同数据库数据保持同步,故数据库SQL脚本非常作用。比如:数据库主脚本,副脚本,增量脚本。 3、 什么是主脚本、副脚本、增量脚本呢? 3.1、主脚本指数据库表或存储过程,视图脚本,序列等脚本。 3.2、副脚本指必须执行主脚本之后才执行的脚本。换句话说在没执行主键脚本的情况下,副脚本执行之后会回滚事务失败。 3.3、增量脚本指在执行主脚本或副脚本之后,根据需求对某个表添加/修改约束(主外键约束,长度约束等),添加/修改字段/添加数据等情况对数据库结构改变处理的一种行为脚本。 1.2作用 1、 快速产出自定义规则需要的SQL脚本。 2、减少人工编SQL脚本出错率问题,完全通过程序检测SQL准确性。 3、帮助开发人员提高SQL效率,减少人工编SQL开发成本问题。 4、帮助开发人员节约时间,同时避免繁琐不必要编SQL的工作。 二、实现方式与原理 2.1实现方式 1、实现方式分:正向与逆向实现。什么是正向与逆行呢【是否有鸡还是有蛋,先后道理同等】 2、正向方式:首先把设计好数据库表文档,把所有表的字段属性配置到EXCEL或者CSV格式的文件通过JXL/POI技术去读取文件的字段,再通过其他技术一系列程序处理之后生成所需要的SQL脚本。 3、逆向方式:首先有数据库表,然后通过ORM持久化技术连接数据库再读取表的字段等属性出来,再通过其他技术一系列程序处理之后生成所需要的SQL脚本。 2.2原理 对数据库软件内置核心表或视图查询出来存储用户行为表结构所有属性信息,对此属性结构信息进行分析与组装所需要SQL脚本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值