当前位置: 首页 > news >正文

什么网站可以做报名系统按效果付费的推广

什么网站可以做报名系统,按效果付费的推广,建设一个网站怎么赚钱,网站开发是什么目录 1 题目2 建表语句3 题解 题目来源:字节跳动。 1 题目 现有订单表t_order,包含订单ID,订单时间,下单用户,当前订单是否有效,请查询出每个用户每笔订单的上一笔有效订单 ----------------------------------------…

目录

  • 1 题目
  • 2 建表语句
  • 3 题解

题目来源:字节跳动。

1 题目


现有订单表t_order,包含订单ID,订单时间,下单用户,当前订单是否有效,请查询出每个用户每笔订单的上一笔有效订单

+---------+----------------------+----------+-----------+
| ord_id  |       ord_time       | user_id  | is_valid  |
+---------+----------------------+----------+-----------+
| 1       | 2023-12-11 12:01:03  | a        | 1         |
| 2       | 2023-12-11 12:02:06  | a        | 0         |
| 3       | 2023-12-11 12:03:15  | a        | 0         |
| 4       | 2023-12-11 12:04:20  | a        | 1         |
| 5       | 2023-12-11 12:05:03  | a        | 1         |
| 6       | 2023-12-11 12:01:02  | b        | 1         |
| 7       | 2023-12-11 12:03:03  | b        | 0         |
| 8       | 2023-12-11 12:04:01  | b        | 1         |
| 9       | 2023-12-11 12:07:03  | b        | 1         |
+---------+----------------------+----------+-----------+

期望查询结果如下:

+---------+----------------------+----------+-----------+--------------------+
| ord_id  |       ord_time       | user_id  | is_valid  | last_valid_ord_id  |
+---------+----------------------+----------+-----------+--------------------+
| 1       | 2023-12-11 12:01:03  | a        | 1         | NULL               |
| 2       | 2023-12-11 12:02:06  | a        | 0         | 1                  |
| 3       | 2023-12-11 12:03:15  | a        | 0         | 1                  |
| 4       | 2023-12-11 12:04:20  | a        | 1         | 1                  |
| 5       | 2023-12-11 12:05:03  | a        | 1         | 4                  |
| 6       | 2023-12-11 12:01:02  | b        | 1         | NULL               |
| 7       | 2023-12-11 12:03:03  | b        | 0         | 6                  |
| 8       | 2023-12-11 12:04:01  | b        | 1         | 6                  |
| 9       | 2023-12-11 12:07:03  | b        | 1         | 8                  |
+---------+----------------------+----------+-----------+--------------------+

2 建表语句


--建表语句
create table t_order
(
ord_id bigint COMMENT '订单ID',
ord_time string COMMENT '订单时间',
user_id string COMMENT '用户',
is_valid bigint COMMENT '订单是否有效'
) COMMENT '订单记录表'
stored as orc
;
-- 数据插入
insert into t_order(ord_id,ord_time,user_id,is_valid)
values
(1,'2023-12-11 12:01:03','a',1),
(2,'2023-12-11 12:02:06','a',0),
(3,'2023-12-11 12:03:15','a',0),
(4,'2023-12-11 12:04:20','a',1),
(5,'2023-12-11 12:05:03','a',1),
(6,'2023-12-11 12:01:02','b',1),
(7,'2023-12-11 12:03:03','b',0),
(8,'2023-12-11 12:04:01','b',1),
(9,'2023-12-11 12:07:03','b',1);

3 题解


(1)先查询出有效订单,然后计算出每笔有效订单的上一单有效订单;

select ord_id,ord_time,user_id,is_valid,lag(ord_id) over (partition by user_id order by ord_time asc) as last_valid_ord_id
from (select ord_id,ord_time,user_id,is_validfrom t_orderwhere is_valid = 1) t

执行结果

+---------+----------------------+----------+-----------+--------------------+
| ord_id  |       ord_time       | user_id  | is_valid  | last_valid_ord_id  |
+---------+----------------------+----------+-----------+--------------------+
| 1       | 2023-12-11 12:01:03  | a        | 1         | NULL               |
| 4       | 2023-12-11 12:04:20  | a        | 1         | 1                  |
| 5       | 2023-12-11 12:05:03  | a        | 1         | 4                  |
| 6       | 2023-12-11 12:01:02  | b        | 1         | NULL               |
| 8       | 2023-12-11 12:04:01  | b        | 1         | 6                  |
| 9       | 2023-12-11 12:07:03  | b        | 1         | 8                  |
+---------+----------------------+----------+-----------+--------------------+

(2)原始的明细数据与新的有效订单表按照用户进行关联,有效订单表的订单时间大于等于原始订单表;

with tmp as (-- 有效订单及其上一单有效记录select ord_id,ord_time,user_id,is_valid,lag(ord_id) over (partition by user_id order by ord_time asc) as last_valid_ord_idfrom (select ord_id,ord_time,user_id,is_validfrom t_orderwhere is_valid = 1) t)
select t1.*,t2.*
from t_order t1
left join tmp t2
on t1.user_id = t2.user_id
where t1.ord_time <= t2.ord_time

执行结果

+------------+----------------------+-------------+--------------+------------+----------------------+-------------+--------------+-----------------------+
| t1.ord_id  |     t1.ord_time      | t1.user_id  | t1.is_valid  | t2.ord_id  |     t2.ord_time      | t2.user_id  | t2.is_valid  | t2.last_valid_ord_id  |
+------------+----------------------+-------------+--------------+------------+----------------------+-------------+--------------+-----------------------+
| 1          | 2023-12-11 12:01:03  | a           | 1            | 1          | 2023-12-11 12:01:03  | a           | 1            | NULL                  |
| 1          | 2023-12-11 12:01:03  | a           | 1            | 4          | 2023-12-11 12:04:20  | a           | 1            | 1                     |
| 2          | 2023-12-11 12:02:06  | a           | 0            | 4          | 2023-12-11 12:04:20  | a           | 1            | 1                     |
| 3          | 2023-12-11 12:03:15  | a           | 0            | 4          | 2023-12-11 12:04:20  | a           | 1            | 1                     |
| 4          | 2023-12-11 12:04:20  | a           | 1            | 4          | 2023-12-11 12:04:20  | a           | 1            | 1                     |
| 1          | 2023-12-11 12:01:03  | a           | 1            | 5          | 2023-12-11 12:05:03  | a           | 1            | 4                     |
| 2          | 2023-12-11 12:02:06  | a           | 0            | 5          | 2023-12-11 12:05:03  | a           | 1            | 4                     |
| 3          | 2023-12-11 12:03:15  | a           | 0            | 5          | 2023-12-11 12:05:03  | a           | 1            | 4                     |
| 4          | 2023-12-11 12:04:20  | a           | 1            | 5          | 2023-12-11 12:05:03  | a           | 1            | 4                     |
| 5          | 2023-12-11 12:05:03  | a           | 1            | 5          | 2023-12-11 12:05:03  | a           | 1            | 4                     |
| 6          | 2023-12-11 12:01:02  | b           | 1            | 6          | 2023-12-11 12:01:02  | b           | 1            | NULL                  |
| 6          | 2023-12-11 12:01:02  | b           | 1            | 8          | 2023-12-11 12:04:01  | b           | 1            | 6                     |
| 7          | 2023-12-11 12:03:03  | b           | 0            | 8          | 2023-12-11 12:04:01  | b           | 1            | 6                     |
| 8          | 2023-12-11 12:04:01  | b           | 1            | 8          | 2023-12-11 12:04:01  | b           | 1            | 6                     |
| 6          | 2023-12-11 12:01:02  | b           | 1            | 9          | 2023-12-11 12:07:03  | b           | 1            | 8                     |
| 7          | 2023-12-11 12:03:03  | b           | 0            | 9          | 2023-12-11 12:07:03  | b           | 1            | 8                     |
| 8          | 2023-12-11 12:04:01  | b           | 1            | 9          | 2023-12-11 12:07:03  | b           | 1            | 8                     |
| 9          | 2023-12-11 12:07:03  | b           | 1            | 9          | 2023-12-11 12:07:03  | b           | 1            | 8                     |
+------------+----------------------+-------------+--------------+------------+----------------------+-------------+--------------+-----------------------+

(3)使用row_number,原始订单记录表中的user_id、ord_id进行分组,按照有效订单表的时间排序,增加分组排序

with tmp as (-- 有效订单及其上一单有效记录select ord_id,ord_time,user_id,is_valid,lag(ord_id) over (partition by user_id order by ord_time asc) as last_valid_ord_idfrom (select ord_id,ord_time,user_id,is_validfrom t_orderwhere is_valid = 1) t)
select t1.*,t2.*,row_number() over (partition by t1.ord_id,t1.user_id order by t2.ord_time asc) as rn
from t_order t1
left join tmp t2
on t1.user_id = t2.user_id
where t1.ord_time <= t2.ord_time

执行结果

+------------+----------------------+-------------+--------------+------------+----------------------+-------------+--------------+-----------------------+-----+
| t1.ord_id  |     t1.ord_time      | t1.user_id  | t1.is_valid  | t2.ord_id  |     t2.ord_time      | t2.user_id  | t2.is_valid  | t2.last_valid_ord_id  | rn  |
+------------+----------------------+-------------+--------------+------------+----------------------+-------------+--------------+-----------------------+-----+
| 1          | 2023-12-11 12:01:03  | a           | 1            | 1          | 2023-12-11 12:01:03  | a           | 1            | NULL                  | 1   |
| 1          | 2023-12-11 12:01:03  | a           | 1            | 4          | 2023-12-11 12:04:20  | a           | 1            | 1                     | 2   |
| 1          | 2023-12-11 12:01:03  | a           | 1            | 5          | 2023-12-11 12:05:03  | a           | 1            | 4                     | 3   |
| 2          | 2023-12-11 12:02:06  | a           | 0            | 4          | 2023-12-11 12:04:20  | a           | 1            | 1                     | 1   |
| 2          | 2023-12-11 12:02:06  | a           | 0            | 5          | 2023-12-11 12:05:03  | a           | 1            | 4                     | 2   |
| 3          | 2023-12-11 12:03:15  | a           | 0            | 4          | 2023-12-11 12:04:20  | a           | 1            | 1                     | 1   |
| 3          | 2023-12-11 12:03:15  | a           | 0            | 5          | 2023-12-11 12:05:03  | a           | 1            | 4                     | 2   |
| 4          | 2023-12-11 12:04:20  | a           | 1            | 4          | 2023-12-11 12:04:20  | a           | 1            | 1                     | 1   |
| 4          | 2023-12-11 12:04:20  | a           | 1            | 5          | 2023-12-11 12:05:03  | a           | 1            | 4                     | 2   |
| 5          | 2023-12-11 12:05:03  | a           | 1            | 5          | 2023-12-11 12:05:03  | a           | 1            | 4                     | 1   |
| 6          | 2023-12-11 12:01:02  | b           | 1            | 6          | 2023-12-11 12:01:02  | b           | 1            | NULL                  | 1   |
| 6          | 2023-12-11 12:01:02  | b           | 1            | 8          | 2023-12-11 12:04:01  | b           | 1            | 6                     | 2   |
| 6          | 2023-12-11 12:01:02  | b           | 1            | 9          | 2023-12-11 12:07:03  | b           | 1            | 8                     | 3   |
| 7          | 2023-12-11 12:03:03  | b           | 0            | 8          | 2023-12-11 12:04:01  | b           | 1            | 6                     | 1   |
| 7          | 2023-12-11 12:03:03  | b           | 0            | 9          | 2023-12-11 12:07:03  | b           | 1            | 8                     | 2   |
| 8          | 2023-12-11 12:04:01  | b           | 1            | 8          | 2023-12-11 12:04:01  | b           | 1            | 6                     | 1   |
| 8          | 2023-12-11 12:04:01  | b           | 1            | 9          | 2023-12-11 12:07:03  | b           | 1            | 8                     | 2   |
| 9          | 2023-12-11 12:07:03  | b           | 1            | 9          | 2023-12-11 12:07:03  | b           | 1            | 8                     | 1   |
+------------+----------------------+-------------+--------------+------------+----------------------+-------------+--------------+-----------------------+-----+

(4)去除冗余字段,筛选rn=1 的记录

with tmp as (-- 有效订单及其上一单有效记录select ord_id,ord_time,user_id,is_valid,lag(ord_id) over (partition by user_id order by ord_time asc) as last_valid_ord_idfrom (select ord_id,ord_time,user_id,is_validfrom t_orderwhere is_valid = 1) t)
select *
from (select t1.*,t2.last_valid_ord_id,row_number() over (partition by t1.ord_id,t1.user_id order by t2.ord_time asc) as rnfrom t_order t1left join tmp t2on t1.user_id = t2.user_idwhere t1.ord_time <= t2.ord_time) tt
where rn = 1

执行结果

+------------+----------------------+-------------+--------------+-----------------------+--------+
| tt.ord_id  |     tt.ord_time      | tt.user_id  | tt.is_valid  | tt.last_valid_ord_id  | tt.rn  |
+------------+----------------------+-------------+--------------+-----------------------+--------+
| 1          | 2023-12-11 12:01:03  | a           | 1            | NULL                  | 1      |
| 2          | 2023-12-11 12:02:06  | a           | 0            | 1                     | 1      |
| 3          | 2023-12-11 12:03:15  | a           | 0            | 1                     | 1      |
| 4          | 2023-12-11 12:04:20  | a           | 1            | 1                     | 1      |
| 5          | 2023-12-11 12:05:03  | a           | 1            | 4                     | 1      |
| 6          | 2023-12-11 12:01:02  | b           | 1            | NULL                  | 1      |
| 7          | 2023-12-11 12:03:03  | b           | 0            | 6                     | 1      |
| 8          | 2023-12-11 12:04:01  | b           | 1            | 6                     | 1      |
| 9          | 2023-12-11 12:07:03  | b           | 1            | 8                     | 1      |
+------------+----------------------+-------------+--------------+-----------------------+--------+
http://www.yayakq.cn/news/141250/

相关文章:

  • 怎么创建自己网站平台个人网站 备案 备注
  • 成品型网站建设西咸新区建设环保网站
  • 做网站的电话厦门网站建设人才
  • 开封市做网站的公司哪里有网站建设加工
  • 网站建设前期目标价格低的英文怎么说
  • 3d网站建设方案天津网站推广有哪些
  • 找做报纸的背景图去什么网站调查队网站建设
  • 校园网站的建设海南 网站开发
  • 网站设计的优化西安抖音代运营公司
  • 制作网站的专业公司广告网络营销策略
  • 徐东网站建设公司wordpress菜单.html
  • 太原h5建站wordpress 4.4.2 漏洞
  • 网站开发分层新手如何做外贸生意
  • 网站域名费用多少钱一年校园网站开发需求文字
  • wordpress站点logo多大合适自学it怎么入门
  • 做网站设计电脑买什么高端本好山西做杂粮的网站
  • wordpress网站搬家图片路径公司软文推广
  • 网站建设和维护工作总结普陀学校网站建设
  • 深圳住房和建设局网站 招标如何用网站开发工具停止网页进程
  • 网站推广名片网站建设及推广方案
  • wordpress如何代码高亮广州seo网站开发
  • 山西长治做网站公司有哪些WordPress主题VR插件
  • 网站开发美学 2.0网站建设流程规划
  • 厦门网站建设培训班什么网站程序好
  • 秦皇岛网站优化360建筑网网址
  • 新网站如何才做被百度收录网站设计与建设公司
  • 大连网站建设微信群网站备案花钱吗
  • 罗岗网站建设公司网站建设方案书微商城
  • 网站内容建设招标盐城市建设局网站设计备案资料
  • 网页 网站 区别国家免费技能培训