大家帮忙思考一个SQL逻辑,连续工作日的发生交易问题

flashstar 2007-09-10 10:36:48
假如我有一个这样的表:
id 发生日期 发生次数
-------------------------
id fsrq fscs
01 20070901 1
02 20070901 1
03 20070901 1
01 20070902 1
02 20070902 3
01 20070903 1
01 20070904 1
02 20070904 1
03 20070904 1
------------------------
现在,我想将fscs>=3,或者每天发生持续3天以上的帐号挑出来,比如说,01和02,
该怎么写这个SQL语句??
...全文
543 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
丐帮小虾 2011-10-21
  • 打赏
  • 举报
回复
我想将fscs> =3,
或者
每天发生持续3天以上的帐号挑出来

2个不是一个意思啊?我是不是钻牛角尖了啊?
1.fscs>=3(给你加上连续的工作日):但是假如第一天是发生一次,第二天发生二次。这个符号条件
2.每天发生持续3天以上:(第一个就不满足了,因为要持续3天)

应该是:找出一个账号,连续3个工作日发生交易次数超过3次的账号找出来(而且要说明一下包不包括周五-周一,算不算工作日)

可能钻牛角尖了。呵呵
丐帮小虾 2011-10-21
  • 打赏
  • 举报
回复
select A.id from (select id ,count(fscs) cishu from tb group by id ) A where A.cishu>=3 这个管不管,只不过可能不是连续工作日
liufuyahong 2007-09-10
  • 打赏
  • 举报
回复
咱也来一个
create table #temp
(
id char(2),
fsrq datetime,
fscs int
)
go

insert into #temp
select '01', '20070901', 1 union all
select '02', '20070901', 1 union all
select '03', '20070901', 1 union all
select '01', '20070902', 1 union all
select '02', '20070902', 3 union all
select '01', '20070903', 1 union all
select '01', '20070904', 1 union all
select '02', '20070904', 1 union all
select '03', '20070904', 1
go

select distinct a.id
from #temp a join #temp b on a.id=b.id and a.fsrq=dateadd(day,1,b.fsrq) or a.fscs>=3
join #temp c on b.id=c.id and b.fsrq=dateadd(day,1,c.fsrq)

go
drop table #temp

/*

(9 row(s) affected)

id
----
01
02

(2 row(s) affected)
*/

flashstar 2007-09-10
  • 打赏
  • 举报
回复
节假日和周末确定是不会有数据的
flashstar 2007-09-10
  • 打赏
  • 举报
回复
Haiwer(海阔天空)兄弟提出的真是一个好思路,谢谢了。

饭后结贴,呵呵
昵称被占用了 2007-09-10
  • 打赏
  • 举报
回复
函数:
create function fn_NextWeekDay(
@Today DateTime
)
returns DateTime
as
begin
return case datepart(weekday,@Today) when 6 then dateadd(day,3,@today)
when 7 then dateadd(day,2,@today)
else dateadd(day,1,@today)
end
end
go

create function fn_PreWeekDay(
@Today DateTime
)
returns DateTime
as
begin
return case datepart(weekday,@Today) when 2 then dateadd(day,-3,@today)
when 1 then dateadd(day,-2,@today)
else dateadd(day,-1,@today)
end
end
go

--
liuzi123 2007-09-10
  • 打赏
  • 举报
回复
路过
昵称被占用了 2007-09-10
  • 打赏
  • 举报
回复
注:20070908是星期六
昵称被占用了 2007-09-10
  • 打赏
  • 举报
回复
问题是,如果星期六 星期日有数据,怎么算,比如如下数据算不算连续

01 20070907 1
01 20070908 1
01 20070910 1


昵称被占用了 2007-09-10
  • 打赏
  • 举报
回复
可以考虑写两个函数,分别返回上一个工作日和下一个工作日

EmeraldSword 2007-09-10
  • 打赏
  • 举报
回复
select distinct id
from @tablename a
where fscs>=3
or (
exists (select 1 from @tablename b
where b.id=a.id
and b.fsrq=dateadd(day,-1,a.fsrq)
and datename(week, a.fsrq)=datename(week, b.fsrq)
)
and exists (select 1 from @tablename b
where b.id=a.id
and b.fsrq=dateadd(day,1,a.fsrq)
and datename(week, a.fsrq)=datename(week, b.fsrq)
)
)
flashstar 2007-09-10
  • 打赏
  • 举报
回复
顶上去
lost_queen 2007-09-10
  • 打赏
  • 举报
回复
如果日期要求是工作日呢?比如说20070907是星期五,20070910是星期一,这两个日期当作是连续的?
------------------------------------------------------------------------------------
加一列吧,给工作日加个标记~
静听高手讨论
EmeraldSword 2007-09-10
  • 打赏
  • 举报
回复
SELECT id
FROM TABLE
GROUP BY id, fsrq
HAVING SUM(fscs)>=3
flashstar 2007-09-10
  • 打赏
  • 举报
回复
谢谢上面两位。
如果日期要求是工作日呢?比如说20070907是星期五,20070910是星期一,这两个日期当作是连续的?
mengmou 2007-09-10
  • 打赏
  • 举报
回复
楼主,这种情况要不要显示?
01 20070901 1
01 20070902 2
dobear_0922 2007-09-10
  • 打赏
  • 举报
回复
create table tb(id varchar(8), fsrq datetime, fscs int)
go

insert tb select '01', '20070901', 1
union all select '02', '20070901', 1
union all select '03', '20070901', 1
union all select '01', '20070902', 1
union all select '02', '20070902', 3
union all select '01', '20070903', 1
union all select '01', '20070904', 1
union all select '02', '20070904', 1
union all select '03', '20070904', 1

select distinct id from tb
where fscs>=3 or
(select count(1) from tb as t where id=tb.id and fscs>0 and abs(datediff(day,fsrq,tb.fsrq))=1)>1


drop table tb

/**************
id
--------
01
02

(2 row(s) affected
****************/
lost_queen 2007-09-10
  • 打赏
  • 举报
回复
Haiwer(海阔天空) 大哥的想法很巧妙啊,赞一下
昵称被占用了 2007-09-10
  • 打赏
  • 举报
回复
--建立测试环境
declare @tablename table(id varchar(9),fsrq smalldatetime,fscs int)
insert @tablename(id,fsrq,fscs)
select '01','20070901','1' union all
select '02','20070901','1' union all
select '03','20070901','1' union all
select '01','20070902','1' union all
select '02','20070902','3' union all
select '01','20070903','1' union all
select '01','20070904','1' union all
select '02','20070904','1' union all
select '03','20070904','1'

select distinct id
from @tablename a
where fscs>=3
or (
exists (select 1 from @tablename b
where b.id=a.id
and b.fsrq=dateadd(day,-1,a.fsrq)
)
and exists (select 1 from @tablename b
where b.id=a.id
and b.fsrq=dateadd(day,1,a.fsrq)
)
)

--结果
id
---------
01
02

(所影响的行数为 2 行)

昵称被占用了 2007-09-10
  • 打赏
  • 举报
回复
select distinct id
from tablename a
where fscs>=3
or (
exists (select 1 from tablename b
where b.id=a.id
and b.fsrq=dateadd(day,-1,a.fsrq)
)
and exists (select 1 from tablename b
where b.id=a.id
and b.fsrq=dateadd(day,1,a.fsrq)
)
)

加载更多回复(3)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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