当前位置:网站首页>【SQL】first_value 应用场景 - 首单 or 复购
【SQL】first_value 应用场景 - 首单 or 复购
2022-07-30 05:42:00 【和风与影】
要准备秋招了,SQL 还是要每天写一写。三天不练手生。
面试的 SQL 题基本都是和岗位的业务有关的场景,最常考察的还是窗口函数。电商场景下经常要统计的一个指标是用户是首单还是复购。首单代表了新的客户来源(新客越多表示营销越成功),复购从侧面翻译了商品的品质(复购越多通常商品越好、性价比高)。这里一道题目就是求每个店铺下面(shop_id)每个订单(order_id)的下单用户(user_id)类别是首单还是复购。
求首单、复购的思路可以用 first_value 这个窗口函数。之前一篇专门讲过函数。
first_value 用于返回第一个值。
select
shop_id,
order_id,
user_id,
-- 首单用 1 表示,复购用 2 表示
if(order_id = first_order_id, 1, 2) as user_category
from
(
select
shop_id,
order_id,
user_id,
-- 一个人可能下过多次单,找出该店铺、该用户粒度下的第一单
first_value(order_id) over (
partition by shop_id,user_id
order by pay_time
) as first_order_id
from order_tb
) as base_order
欢迎点击此处关注公众号。
边栏推荐
- 【问题解决】在写CSDN博客时,如何对段落进行首行缩进?
- strcasecmp和strncasecmp
- Deserialization character escape
- CTF之misc-其他类型隐写
- jsonpath
- 3 minutes to tell you how to become a hacker | Zero foundation to hacker introductory guide, you only need to master these five skills
- js方法 reduce 用法
- 典型线程问题综合演示
- sqli-labs less3/4 Targeting Notes
- let到底会不会造成变量提升
猜你喜欢
easyexcel使用教程-导出篇
JDBC programming of MySQL database
DVWA安装教程(懂你的不懂·详细)
FastAPI 快速入门
Connect to Mysql in the cloud server Docker detailed graphic and text operations (full)
猜数字小游戏(随机生成’三剑客‘)
Application Practice | Application Practice of Apache Doris in Baidu Intelligent Cloud Billing System
VS2022中关于scanf函数报错解决方法
Understand JDBC in one article
标准输入输出流(System.in,System.out)
随机推荐
P3 元宝序列化笔记
【小程序项目开发-- 京东商城】uni-app之分类导航区域
3 minutes to tell you how to become a hacker | Zero foundation to hacker introductory guide, you only need to master these five skills
art-template模板引擎过滤器的使用【入门简单使用篇】
强国杯初赛WP
uni-app: The use of uni-icons and how to customize icons
[网鼎杯 2020 青龙组]AreUSerialz
CTF之misc-图片隐写
在弹性布局flex布局中,行内标签也能直接加宽高
连接云服务器Docker中的Mysql 详细图文操作(全)
js方法 reduce 用法
一类SMS漏洞的防御思路
Communication middleware Fast DDS basic concepts and communication examples
awd——waf部署
C#下利用开源NPlot绘制股票十字交叉线
C#下大批量一键空投实现
js 去除掉对象中的null,‘‘,[],{}
Misc of CTF-Memory Analysis (Volatility)
攻防世界easy_web
Arrays工具类的使用