在一个时间段内进行频度统计的问题,100分急求答案!

streetgrass 2010-05-01 05:00:03
我有一张表是记录每日的交易明细的,表结构如下:客户号,交易日期,交易金额.

现在需要统计在10内有超过20次交易的客户是哪些,请问SQL应该怎么写?
...全文
109 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouwei7682719 2010-05-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fredrickhu 的回复:]
SQL code
select
客户号,count(1) as 交易次数
from
tb
where
交易日期 between dateadd(dd,-10,getdate()) and getdate()
group by
客户号
having
count(1)>=20
[/Quote]恩, 此方法可行!
lovezx1028 2010-05-03
  • 打赏
  • 举报
回复
ding yixia ..
zheninchangjiang 2010-05-03
  • 打赏
  • 举报
回复
交易日期>=a.交易日期 and 交易日期<=dateadd(day,9,a.交易日期)
头尾都等,要减少一天
dawugui 2010-05-02
  • 打赏
  • 举报
回复
[Quote=引用楼主 streetgrass 的回复:]
我有一张表是记录每日的交易明细的,表结构如下:客户号,交易日期,交易金额.

现在需要统计在10内有超过20次交易的客户是哪些,请问SQL应该怎么写?
[/Quote]
--如果是最近十天.
select 客户号 from tb where datediff(dd,交易日期,getdate()) <= 10 group by 客户号 having count(1) >= 20

--如果是任意十天.
帮顶.
zheninchangjiang 2010-05-02
  • 打赏
  • 举报
回复
是有些问题,
交易日期 between a.交易日期 and dateadd(day,10,a.交易日期)
或者: 交易日期>=a.交易日期 and 交易日期<=dateadd(day,10,a.交易日期)
csw200201 2010-05-02
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 xys_777 的回复:]

引用 8 楼 zheninchangjiang 的回复:
--case 2
select distinct 客户号 from tablename a
where (select count(*) from tablename where 客户号=a.客户号 and dateadd(day,10,交易日期)<=a.交易日期)>20

这个不错,支持
[/Quote]

This one doesn't actually work correctly. 1 Jan 2010 vs 1 May 2010 would make the dateadd(day,10,交易日期)<=a.交易日期) expression true but it would certainly fall outside 10 days requirement.
永生天地 2010-05-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zheninchangjiang 的回复:]
--case 2
select distinct 客户号 from tablename a
where (select count(*) from tablename where 客户号=a.客户号 and dateadd(day,10,交易日期)<=a.交易日期)>20
[/Quote]
这个不错,支持
zheninchangjiang 2010-05-01
  • 打赏
  • 举报
回复
--case 2
select distinct 客户号 from tablename a
where (select count(*) from tablename where 客户号=a.客户号 and dateadd(day,10,交易日期)<=a.交易日期)>20

y_dong119 2010-05-01
  • 打赏
  • 举报
回复
select 客户号,count(1) as 交易次数
from table1 where 交易日期>=convert(varchar(8),DATEADD(DD,-10,GETDATE()),112)
group by 客户号 having count(客户号)>20
nalnait 2010-05-01
  • 打赏
  • 举报
回复
group by ...having count(1)>20
csw200201 2010-05-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 rmljoe 的回复:]

“在10日内有超过20次交易” 不明确,有两种可能
1)以某一天往前推10日内超过20次交易
2)所有数据里任意10日内只要超过20次交易
如果是第二种可就难算了
[/Quote]

Plenty of answers on how to deal with scenario 1

Here is a method of achieving scenario 2:


create table customer_trans
(
trans_id int primary key,
customer_id int,
tran_date datetime,
tran_amount numeric(16, 4)
)

select
distinct a.customer_id
from
customer_trans a
where
coalesce((
select
count(*)
from
customer_trans
where
customer_id=a.customer_id and
tran_date>=a.tran_date and
tran_date<dateadd(d, 10, a.tran_date)
),0)>=20
喜-喜 2010-05-01
  • 打赏
  • 举报
回复
--最近十天客户交易情况
select 客户号,count(1) as 交易次数
from table where 交易日期>=dateadd(dd,-10,getdate())
group by 客户号 having count(客户号)>20
rmljoe 2010-05-01
  • 打赏
  • 举报
回复
“在10日内有超过20次交易” 不明确,有两种可能
1)以某一天往前推10日内超过20次交易
2)所有数据里任意10日内只要超过20次交易
如果是第二种可就难算了
chuifengde 2010-05-01
  • 打赏
  • 举报
回复
select 客户号 from [Table]
where 时间条件
group by 客户号 having count(1)>20
--小F-- 2010-05-01
  • 打赏
  • 举报
回复
select
客户号,count(1) as 交易次数
from
tb
where
交易日期 between dateadd(dd,-10,getdate()) and getdate()
group by
客户号
having
count(1)>=20

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧