SQL创建表,表名:t_member_consume [consume_date] [user_id] 2019-01-01 1 2019-01-02 1 2019-01-01 2 2019-01-02 2 2019-01-03 2 2019-01-01 3 2019-01-01 4 2019-01-01 5 2019-01-02 5 2019-01-03 5,接着用SQL查询连续三天到店消费的用户
时间: 2023-02-27 11:45:32 浏览: 80
您可以使用以下 SQL 语句创建 t_member_consume 表:
```
CREATE TABLE t_member_consume (
consume_date date,
user_id int
);
```
然后您可以使用以下 SQL 语句向表中插入数据:
```
INSERT INTO t_member_consume (consume_date, user_id)
VALUES ('2019-01-01', 1), ('2019-01-02', 1),
('2019-01-01', 2), ('2019-01-02', 2), ('2019-01-03', 2),
('2019-01-01', 3),
('2019-01-01', 4),
('2019-01-01', 5), ('2019-01-02', 5), ('2019-01-03', 5);
```
最后,您可以使用以下 SQL 语句查询连续三天到店消费的用户:
```
WITH cte AS (
SELECT user_id, consume_date,
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY consume_date) -
ROW_NUMBER() OVER (ORDER BY user_id, consume_date) AS grp
FROM t_member_consume
)
SELECT user_id
FROM cte
GROUP BY user_id, grp
HAVING COUNT(*) >= 3;
```
这条语句会返回连续三天到店消费的用户的 user_id。
阅读全文