求一个sql语句,同时满足多个条件的,在线等

Houjun_pyc 2012-04-20 06:01:13
就一个这样的简单的表
id starttime
1 2012-04-01 08:00:00
1 2012-04-01 15:20:00
1 2012-04-02 00:50:00
1 。。。
2 。。。
2 。。。
3 。。。

需求:我想查询一个 时间段内 每个id 发生 特定情况 的 次数
这个特定情况为:同一个id 在这个表中 当天的5:30:00到14:30:00有记录,并且在当天的14:30:01到第二天的14:30:00也有记录,那么就记一次。

最后要查询,在这个时间段内,每个id共发生几次这种情况

例如:上面的这个表中,1这个id在2012-04-01当天5:30:00到14:30:00有一条记录,在当天的14:30:01到第二天的14:30:00有两条记录,那么1这个id在2012-04-01到2012-04-02这个时间段内就有一次这个情况。

所以我就想要一个可以在任意一个时间段内(如2012-04-01到2012-04-20),查询每个id共发生这种情况几次的语句

结果: id 次数
1 2
。。 。。
...全文
959 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tjs_125 2012-04-24
  • 打赏
  • 举报
回复
IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb;
GO
CREATE TABLE tb(id int, mid int, starttime datetime);
INSERT INTO tb(id, starttime)
select 1, '2012-04-01 08:00:00' union all
select 1, '2012-04-01 15:20:00' union all
select 1, '2012-04-02 00:50:00' union all
select 1, '2012-04-02 8:50:00' union all
select 1, '2012-04-02 18:50:00' union all
select 1, '2012-04-03 5:50:00' union all
select 2, '2012-04-04 5:50:00' union all
select 2, '2012-04-04 8:50:00' union all
select 2, '2012-04-04 15:50:00';

--select id, starttime from tb;

select a.id, sum([count]) as 次数
from ( select id, max(num) as [Count]
from (
select a.id, a.starttime, 1 as num
from tb a
where a.starttime >= '2012-04-01' and a.starttime <= '2012-04-12'
and convert(varchar(8), a.starttime, 108) between '05:30:00' and '14:30:00'
and (exists ( select 1 from tb b
where a.id = b.id
and b.starttime between cast( convert(varchar(10), a.starttime, 120) + ' 05:30:00' as datetime)
and cast( convert(varchar(10), a.starttime, 120) + ' 14:30:00' as datetime)
)
)
and (exists ( select 1 from tb b1
where a.id = b1.id
and b1.starttime > cast( convert(varchar(10), a.starttime, 120) + ' 14:30:00' as datetime)
and b1.starttime <= cast( convert(varchar(10), a.starttime + 1, 120) + ' 14:30:00' as datetime)
)
)
) t
group by id, convert(varchar(10), starttime, 120)
) a
group by a.id
tjs_125 2012-04-23
  • 打赏
  • 举报
回复
那你把T的内容改为子查询就好了。
Houjun_pyc 2012-04-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

SQL code
/*
id starttime
1 2012-04-01 08:00:00
1 2012-04-01 15:20:00
1 2012-04-02 00:50:00
当天的5:30:00到14:30:00有记录,
并且在当天的14:30:01到第二天的14:30:00也有记录,那么就记一次
*/
IF OBJECT_ID('tb') IS ……
[/Quote]

运行时提示我在关键字 'with' 附近有语法错误。
我是sql server 2000。
Houjun_pyc 2012-04-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
方法一:
select id ,sum(1) from tb a where starttime >开始时间 and starttime <截止时间
and (exists (select id from tb b where a.id=b.id
and depart("HH:mm:ss",starttime) >'05:30:00'
and depart("HH:mm:ss"……
[/Quote]

对于方法一,你这个我运行有错误,depart不能识别,改成convert后也有问题。
对于方法二,每个数据都有用处的,不能因为有了就不插入数据库了,因为查询详情的时候希望看到所有的数据。
Houjun_pyc 2012-04-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

select * from (select aa,sum(1) as t1aa from dbo.aaaa where bb>='2012-4-1 8:00:00' and bb<='2012-4-1 14:30:00'
GROUP BY aa
) t1
union all
select * from (select aa,sum(1) as t2aa from dbo.aaaa……
[/Quote]

你这个不对吧,查询的是在某时间段内,你这个也没有时间段啊!
Houjun_pyc 2012-04-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

时间段 事先给定还是 统计时候确定的??
[/Quote]


时间段是查询的时候任意给的,想查什么时间段,就写什么时间段的。
rory105 2012-04-22
  • 打赏
  • 举报
回复
求解,学习中
tjs_125 2012-04-22
  • 打赏
  • 举报
回复
/*
id 次数
----------- -----------
1 2
2 1
*/
tjs_125 2012-04-22
  • 打赏
  • 举报
回复
/*
id starttime
1 2012-04-01 08:00:00
1 2012-04-01 15:20:00
1 2012-04-02 00:50:00
当天的5:30:00到14:30:00有记录,
并且在当天的14:30:01到第二天的14:30:00也有记录,那么就记一次
*/
IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb;
GO
CREATE TABLE tb(id int, mid int, starttime datetime);
INSERT INTO tb(id, starttime)
select 1, '2012-04-01 08:00:00' union all
select 1, '2012-04-01 15:20:00' union all
select 1, '2012-04-02 00:50:00' union all
select 1, '2012-04-02 8:50:00' union all
select 1, '2012-04-02 18:50:00' union all
select 1, '2012-04-03 5:50:00' union all
select 2, '2012-04-04 5:50:00' union all
select 2, '2012-04-04 8:50:00' union all
select 2, '2012-04-04 15:50:00';

--select id, starttime from tb;
with T as
(
select a.id, a.starttime, 1 as num
from tb a
where a.starttime >= '2012-04-01' and a.starttime <= '2012-04-12'
and convert(varchar(8), a.starttime, 108) between '05:30:00' and '14:30:00'
and (exists ( select 1 from tb b
where a.id = b.id
and b.starttime between cast( convert(varchar(10), a.starttime, 120) + ' 05:30:00' as datetime)
and cast( convert(varchar(10), a.starttime, 120) + ' 14:30:00' as datetime)
)
)
and (exists ( select 1 from tb b1
where a.id = b1.id
and b1.starttime > cast( convert(varchar(10), a.starttime, 120) + ' 14:30:00' as datetime)
and b1.starttime <= cast( convert(varchar(10), a.starttime + 1, 120) + ' 14:30:00' as datetime)
)
)
)
select a.id, sum([count]) as 次数
from ( select id, max(num) as [Count] from T
group by id, convert(varchar(10), starttime, 120)
) a
group by a.id
liuhzdingxm 2012-04-21
  • 打赏
  • 举报
回复
select * from (select aa,sum(1) as t1aa from dbo.aaaa where bb>='2012-4-1 8:00:00' and bb<='2012-4-1 14:30:00'
GROUP BY aa
) t1
union all
select * from (select aa,sum(1) as t2aa from dbo.aaaa where bb>'2012-4-1 14:30:00' and bb<=dateadd(d,1,'2012-4-1 14:30:00')
GROUP BY aa
) t2
@信心 2012-04-21
  • 打赏
  • 举报
回复

方法一:
select id ,sum(1) from tb a where starttime >开始时间 and starttime <截止时间
and (exists (select id from tb b where a.id=b.id
and depart("HH:mm:ss",starttime) >'05:30:00'
and depart("HH:mm:ss",starttime) <'14:30:00' )
and (exists (select id from tb b1 where a.id=b1.id
and depart("HH:mm:ss",starttime) >'14:30:00'
and dateadd(d,1,depart("HH:mm:ss",starttime))<='14:30:00'
)
group by id



)


方法二:可换角度呀! 在往数据表insert into 时,判断在特定时间里有无记录,有则不再执行insert
后期统计也方便,也节约表空间。
MRKDTOPONE 2012-04-20
  • 打赏
  • 举报
回复
时间段 事先给定还是 统计时候确定的??

22,207

社区成员

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

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