sql查询连续3天有交易记录的客户

利用表的自关联查询

表A

CUS_ID TXN_DT ID
1 20180101 1
2 20180101 2
3 20180101 3
1 20180102 4
2 20180102 5
2 20180102 6
1 20180103 7
3 20180103 8
with t as (
    select
        cus_id
        ,txn_dt
    from a
    qualify row_number()over(partition by cus_id,txn_dt order by id ) = 1
              )
select
    cus_id
from t m
inner join t n
on m.cus_id = n.cus_id
and m.txn_dt <= n.txn_dt +2
group by 1
having count(cus_id) = 3
;
--或者
select
    cus_id
from a m
inner join a n
on m.cus_id = n.cus_id
and m.txn_dt <= n.txn_dt +2
group by 1
having count(distinct txn_dt) = 3
;

  

原文地址:https://www.cnblogs.com/king-cobra-rko/p/11167458.html

时间: 2024-08-30 09:49:32

sql查询连续3天有交易记录的客户的相关文章

Sql 查询当天、本周、本月记录

Sql 查询当天.本周.本月记录--查询当天: [sql] view plaincopyprint?select * from info where DateDiff(dd,datetime,getdate())=0 --查询24小时内的: [sql] view plaincopyprint?select * from info where DateDiff(hh,datetime,getDate())<=24 --info为表名,datetime为数据库中的字段值 --查询当天:[sql] v

执行一次SQL查询,修改UPDATE多行记录

通常情况下,我们会使用以下SQL语句来更新字段值: UPDATE mytable SET myfield='value' WHERE other_field='other_value';UPDATE mytable SET myfield='value' WHERE other_field='other_value'; 但是,如果你想更新多行数据,并且每行记录的各字段值都是各不一样,你会怎么办呢?举个例子,我的博客有三个分类目录(免费资源.教程指南.橱窗展示),这些分类目录的信息存储在数据库表c

sql 查询某个字段最长的记录

sql  查询文本字段中值的长度最长的记录 一.函数1.SQL ServerLEN() 函数返回文本字段中值的长度.SELECT LEN(column_name) FROM table_name;2.MySQL LENGTH() 函数返回文本字段中值的长度.SELECT LENGTH(column_name) FROM table_name; 二.简单用法1.SQL Serverselect `字段`, len(`字段`) from 表名 where len(`字段`) = ( select m

SQL 查询每组的第一条记录

CREATE TABLE [dbo].[test1]( [program_id] [int] NULL, [person_id] [int] NULL ) ON [PRIMARY] /*查询每组分组中第一条记录*/ select * from test1 as a where a.person_id in ( select top 1 person_id from test1 where program_id = a.program_id); select * from ( select ROW

Sql 查询当天、本周、本月记录、上周、上月记录

查询当天: select * from info where DateDiff(dd,datetime,getdate())=0 查询24小时内: select * from info where DateDiff(hh,datetime,getDate())<=24 查询当天: select * from table where DateDiff(dd,datetime,getdate())=0 本月记录 : SELECT * FROM 表 WHERE datediff(month,[date

使用一条sql查询多个表中的记录数

方法一: select t1.num1,t2.num2,t3.num3 from (select count(*) num1 from table1) t1, (select count(*) num2 from table2) t2, (select count(*) num3 from table3) t3 方法二: select sum(t.num1),sum(t.num2),sum(t.num3) from ( select count(*) num1,0 as num2,0 as nu

sql查询数据库中所有表的记录条数,以及占用磁盘空间大小。

1 SELECT 2 TableName = obj.name, 3 TotalRows = prt.rows, 4 [SpaceUsed(KB)] = SUM(alloc.used_pages)*8 5 FROM sys.objects obj 6 JOIN sys.indexes idx on obj.object_id = idx.object_id 7 JOIN sys.partitions prt on obj.object_id = prt.object_id 8 JOIN sys.

一个有趣的 SQL 查询(查询7天连续登陆)

一个有趣的 SQL 查询 一个朋友有这样一个SQL查询需求: 有一个登录表(tmp_test),包含用户ID(uid)和登录时间(login_time).表结构如下: *************************** 1. row *************************** Field: uid Type: int(10) unsigned Null: NO Key: MUL Default: NULL Extra: *************************** 2.

SQL查询多条不重复记录值简要解析【转载】

转载http://hi.baidu.com/my_favourate/item/3716b0cbe125f312505058eb SQL查询多条不重复记录值简要解析2008-02-28 11:36 以下内容在Oracle 10g下测试由于设计需要,需要查询不重复的记录值,同类问题,想必大家都遇到过,于是乎马上GOOGLE一下,发现此类问题还挺多,解决方案也不少,仔细看看.例如有如下表结构和值tablefid   name sex1     a      男2     b      男3