初学,需要大家帮忙写一个group by查询时间的sql,非常感谢!

apple749769 2011-10-14 09:03:45
会议表A:
NO(会议室编号) startTime(创建时间) endTime(结束时间)
1 2011-10-13 08:10:00 2011-10-13 08:15:00 主持人
1 2011-10-13 08:12:00 2011-10-13 08:19:00 参与者
1 2011-10-13 08:14:00 2011-10-13 08:20:00 参与者
1 2011-10-13 08:13:00 2011-10-13 08:14:00 参与者

2 2011-10-13 08:17:00 2011-10-13 08:21:00 主持人
2 2011-10-13 08:18:00 2011-10-13 08:20:00 参与者
2 2011-10-13 08:19:00 2011-10-13 08:22:00 参与者

我需要查询出所有会议室下的开始创建时间(最早开始),主持人退出时间,会议室关闭时间(最晚结束)
结果应该是这样:
1 2011-10-13 08:10:00 2011-10-13 08:15:00 2011-10-13 08:20:00
2 2011-10-13 08:17:00 2011-10-13 08:21:00 2011-10-13 08:22:00

在线等待,有答案,立即给分结贴!
...全文
154 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
apple749769 2011-10-20
  • 打赏
  • 举报
回复
非常感谢大家帮我解决问题,谢谢!
kensouterry 2011-10-15
  • 打赏
  • 举报
回复
哦 忘记告诉你了,SQL2000不能使用CTE表达式和Row_Number()函数
嘿嘿……
kensouterry 2011-10-15
  • 打赏
  • 举报
回复


create table #tb(NO int,startTime datetime,endTime datetime)
insert into #tb select 1,'2011-10-13 08:10:00','2011-10-13 08:15:00'
insert into #tb select 1,'2011-10-13 08:12:00','2011-10-13 08:19:00'
insert into #tb select 1,'2011-10-13 08:14:00','2011-10-13 08:20:00'
insert into #tb select 1,'2011-10-13 08:13:00','2011-10-13 08:14:00'
insert into #tb select 2,'2011-10-13 08:17:00','2011-10-13 08:21:00'
insert into #tb select 2,'2011-10-13 08:18:00','2011-10-13 08:20:00'
insert into #tb select 2,'2011-10-13 08:19:00','2011-10-13 08:22:00'
go

;with k as
(
--对每个组和时间进行排序
Select No,startTime, endtime, ROW_NUMBER() over(partition by No order by No, starttime) as FreshID
from #tb
)
,
k_end as
(
select No, MAX(endTime) as endTime
from k
group by NO
)
select k.NO, k.startTime, k_end.endTime
from k
JOIN k_end
ON K.NO=k_end.NO
where k.FreshID=1

drop table #tb
go


运行下看可以满足你的要求不,不符合说!
--小F-- 2011-10-14
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 qqzhengwei 的回复:]
数据借楼上的哈

SQL code

create table #tb(NO int,startTime datetime,endTime datetime)
insert into #tb select 1,'2011-10-13 08:10:00','2011-10-13 08:15:00'
insert into #tb select 1,'2011-10-13 08:12:00……
[/Quote]
不知道是不是这个意思
pengxuan 2011-10-14
  • 打赏
  • 举报
回复

if object_id('tb','U') is not null
drop table tb
go
create table tb
(
NO int,
startTime datetime,
endTime datetime
)
go
insert into tb
select 1,'2011-10-13 08:10:00','2011-10-13 08:15:00' union all
select 1,'2011-10-13 08:12:00','2011-10-13 08:19:00' union all
select 1,'2011-10-13 08:14:00','2011-10-13 08:20:00' union all
select 1,'2011-10-13 08:13:00','2011-10-13 08:14:00' union all
select 2,'2011-10-13 08:17:00','2011-10-13 08:21:00' union all
select 2,'2011-10-13 08:18:00','2011-10-13 08:20:00' union all
select 2,'2011-10-13 08:19:00','2011-10-13 08:22:00'
go
select *,(select max(endTime) from tb where NO=a.NO) from tb a where not exists(select 1 from tb where NO=a.NO and startTime<a.startTime)
薇薇 2011-10-14
  • 打赏
  • 举报
回复
数据借楼上的哈

create table #tb(NO int,startTime datetime,endTime datetime)
insert into #tb select 1,'2011-10-13 08:10:00','2011-10-13 08:15:00'
insert into #tb select 1,'2011-10-13 08:12:00','2011-10-13 08:19:00'
insert into #tb select 1,'2011-10-13 08:14:00','2011-10-13 08:20:00'
insert into #tb select 1,'2011-10-13 08:13:00','2011-10-13 08:14:00'
insert into #tb select 2,'2011-10-13 08:17:00','2011-10-13 08:21:00'
insert into #tb select 2,'2011-10-13 08:18:00','2011-10-13 08:20:00'
insert into #tb select 2,'2011-10-13 08:19:00','2011-10-13 08:22:00'


--//
我需要查询出所有会议室下的开始创建时间(最早开始),主持人退出时间,会议室关闭时间(最晚结束)
结果应该是这样:
1 2011-10-13 08:10:00 2011-10-13 08:15:00 2011-10-13 08:20:00
2 2011-10-13 08:17:00 2011-10-13 08:21:00 2011-10-13 08:22:00

--//先对同一no进行排序,第一个也就是row=1的endtime就是主持人的退出时间,对大的结束时间就是会场的关闭时间
;with b as (
select *,row=row_number() over(partition by no order by starttime) from #tb
)
select b.no,min(b.starttime)as '开始创建时间(最早开始)',
max(case when row=1 then b.endtime end) as '主持人退出时间'
,max(b.endtime)as '会议室关闭时间(最晚结束'
from b
group by b.no
--//结果
/*
no 开始创建时间(最早开始) 主持人退出时间 会议室关闭时间(最晚结束
----------- ----------------------- ----------------------- -----------------------
1 2011-10-13 08:10:00.000 2011-10-13 08:15:00.000 2011-10-13 08:20:00.000
2 2011-10-13 08:17:00.000 2011-10-13 08:21:00.000 2011-10-13 08:22:00.000
警告: 聚合或其他 SET 操作消除了空值。

(2 行受影响)
*/
小宏 2011-10-14
  • 打赏
  • 举报
回复
lz是不是拷贝少了东西了啊,晴天的2个方法应该都没有问题的!
-晴天 2011-10-14
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 apple749769 的回复:]
to qianjin036a
:返回错误信息:
消息 512,级别 16,状态 1,第 1 行
子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
[/Quote]

哥们,我都测试完了,不可能的啊.
apple749769 2011-10-14
  • 打赏
  • 举报
回复
to qianjin036a
:返回错误信息:
消息 512,级别 16,状态 1,第 1 行
子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
-晴天 2011-10-14
  • 打赏
  • 举报
回复
or:
create table tb(NO int,startTime datetime,endTime datetime)
insert into tb select 1,'2011-10-13 08:10:00','2011-10-13 08:15:00'
insert into tb select 1,'2011-10-13 08:12:00','2011-10-13 08:19:00'
insert into tb select 1,'2011-10-13 08:14:00','2011-10-13 08:20:00'
insert into tb select 1,'2011-10-13 08:13:00','2011-10-13 08:14:00'
insert into tb select 2,'2011-10-13 08:17:00','2011-10-13 08:21:00'
insert into tb select 2,'2011-10-13 08:18:00','2011-10-13 08:20:00'
insert into tb select 2,'2011-10-13 08:19:00','2011-10-13 08:22:00'
go
select no,
Min(startTime) as 最早时间,
(select endTime from tb where starttime=(select min(starttime) from tb where no=a.no))as 主持人退出时间,
MAX(endTime) as 最晚时间
from tb a
group by no
/*
no 最早时间 主持人退出时间 最晚时间
----------- ----------------------- ----------------------- -----------------------
1 2011-10-13 08:10:00.000 2011-10-13 08:15:00.000 2011-10-13 08:20:00.000
2 2011-10-13 08:17:00.000 2011-10-13 08:21:00.000 2011-10-13 08:22:00.000

(2 行受影响)
*/
go
drop table tb
-晴天 2011-10-14
  • 打赏
  • 举报
回复
create table tb(NO int,startTime datetime,endTime datetime)
insert into tb select 1,'2011-10-13 08:10:00','2011-10-13 08:15:00'
insert into tb select 1,'2011-10-13 08:12:00','2011-10-13 08:19:00'
insert into tb select 1,'2011-10-13 08:14:00','2011-10-13 08:20:00'
insert into tb select 1,'2011-10-13 08:13:00','2011-10-13 08:14:00'
insert into tb select 2,'2011-10-13 08:17:00','2011-10-13 08:21:00'
insert into tb select 2,'2011-10-13 08:18:00','2011-10-13 08:20:00'
insert into tb select 2,'2011-10-13 08:19:00','2011-10-13 08:22:00'
go
select no,
Min(startTime) as 最早时间,
Min(case when rn=1 then endTime end) as 主持人退出时间,
MAX(endTime) as 最晚时间
from (select row_number()over(partition by no order by starttime)rn,* from tb)t
group by no
/*
no 最早时间 主持人退出时间 最晚时间
----------- ----------------------- ----------------------- -----------------------
1 2011-10-13 08:10:00.000 2011-10-13 08:15:00.000 2011-10-13 08:20:00.000
2 2011-10-13 08:17:00.000 2011-10-13 08:21:00.000 2011-10-13 08:22:00.000
警告: 聚合或其他 SET 操作消除了 Null 值。

(2 行受影响)
*/
go
drop table tb
apple749769 2011-10-14
  • 打赏
  • 举报
回复
楼上Sql试过了,不正确,麻烦再试试。。。非常感谢!
xiangaylian 2011-10-14
  • 打赏
  • 举报
回复
如果最早进场的一定是主持人的话。。

create table #tb(NO int,startTime datetime,endTime datetime)
insert into #tb select 1,'2011-10-13 08:10:00','2011-10-13 08:15:00'
insert into #tb select 1,'2011-10-13 08:12:00','2011-10-13 08:19:00'
insert into #tb select 1,'2011-10-13 08:14:00','2011-10-13 08:20:00'
insert into #tb select 1,'2011-10-13 08:13:00','2011-10-13 08:14:00'
insert into #tb select 2,'2011-10-13 08:17:00','2011-10-13 08:21:00'
insert into #tb select 2,'2011-10-13 08:18:00','2011-10-13 08:20:00'
insert into #tb select 2,'2011-10-13 08:19:00','2011-10-13 08:22:00'
go
select no,
Min(startTime) as 最早时间,
(select endTime from #tb where startTime = (select min(startTime) from #tb where no = t.no) and no = t.no) as 主持人退出时间,
MAX(endTime) as 最晚时间
from #tb t
group by no
小宏 2011-10-14
  • 打赏
  • 举报
回复
借晴天哥数据一用呵呵!

create table #tb(NO int,startTime datetime,endTime datetime)
insert into #tb select 1,'2011-10-13 08:10:00','2011-10-13 08:15:00'
insert into #tb select 1,'2011-10-13 08:12:00','2011-10-13 08:19:00'
insert into #tb select 1,'2011-10-13 08:14:00','2011-10-13 08:20:00'
insert into #tb select 1,'2011-10-13 08:13:00','2011-10-13 08:14:00'
insert into #tb select 2,'2011-10-13 08:17:00','2011-10-13 08:21:00'
insert into #tb select 2,'2011-10-13 08:18:00','2011-10-13 08:20:00'
insert into #tb select 2,'2011-10-13 08:19:00','2011-10-13 08:22:00'

;with t as
(
select row_number() over(partition by no order by startTime ) as type,
* from #tb
)
select
no,
(select min(startTime) from t where no = a.no and type = 1) as '开始创建时间',
(select max(endTime) from t where no = a.no and type = 1) as '主持人退出时间',
(select max(endTime) from t where no = a.no and type != 1) as '会议室关闭时间'
from t a
group by no

drop table #tb

----------------------------------------
no 开始创建时间 主持人退出时间 会议室关闭时间
----------- ----------------------- ----------------------- -----------------------
1 2011-10-13 08:10:00.000 2011-10-13 08:15:00.000 2011-10-13 08:20:00.000
2 2011-10-13 08:17:00.000 2011-10-13 08:21:00.000 2011-10-13 08:22:00.000

(2 行受影响)
Lemon2050 2011-10-14
  • 打赏
  • 举报
回复
修改下:

select b.no,
Min(b.startTime) as 最早时间,
(select a.endTime from tb a group by a.no having a.no=b.no and a.startTime= 最早时间) as 主持人退出时间,
MAX(b.endTime) as 最晚时间
from tb b
group by b.no

Lemon2050 2011-10-14
  • 打赏
  • 举报
回复
select b.no,
Min(b.startTime) as 最早时间,
(select a.endTime from tb a and group by a.no having a.no=b.no and a.startTime= 最早时间) as 主持人退出时间,
MAX(b.endTime) as 最晚时间
from tb b
group by b.no
apple749769 2011-10-14
  • 打赏
  • 举报
回复
楼上兄弟错了,那个最后的写的主持人,参与者不是字段的值,是我的注释,
主持人的唯一标识是创建时间最小。帮忙再看看
-晴天 2011-10-14
  • 打赏
  • 举报
回复
create table tb(NO int,startTime datetime,endTime datetime,人员 nvarchar(5))
insert into tb select 1,'2011-10-13 08:10:00','2011-10-13 08:15:00','主持人'
insert into tb select 1,'2011-10-13 08:12:00','2011-10-13 08:19:00','参与者'
insert into tb select 1,'2011-10-13 08:14:00','2011-10-13 08:20:00','参与者'
insert into tb select 1,'2011-10-13 08:13:00','2011-10-13 08:14:00','参与者'
insert into tb select 2,'2011-10-13 08:17:00','2011-10-13 08:21:00','主持人'
insert into tb select 2,'2011-10-13 08:18:00','2011-10-13 08:20:00','参与者'
insert into tb select 2,'2011-10-13 08:19:00','2011-10-13 08:22:00','参与者'
go
select no,
Min(startTime) as 最早时间,
Max(case when 人员='主持人' then endTime end) as 主持人退出时间,
MAX(endTime) as 最晚时间
from tb
group by no
/*
no 最早时间 主持人退出时间 最晚时间
----------- ----------------------- ----------------------- -----------------------
1 2011-10-13 08:10:00.000 2011-10-13 08:15:00.000 2011-10-13 08:20:00.000
2 2011-10-13 08:17:00.000 2011-10-13 08:21:00.000 2011-10-13 08:22:00.000
警告: 聚合或其他 SET 操作消除了 Null 值。

(2 行受影响)
*/
go
drop table tb

34,590

社区成员

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

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