去除规定时间内数据

kobaer 2011-04-29 05:04:02

有如下一些字段
id name time
1 AAA 2011-4-25 8:43:04
2 AAA 2011-4-25 9:15:09
3 BBB 2011-4-25 9:43:10
4 AAA 2011-4-25 9:43:01
5 BBB 2011-4-25 9:49:22
6 CCC 2011-4-25 9:50:24
7 AAA 2011-4-25 11:50:44





我想取出如下数据(规则,在一个小时内出现的数据只统计一次)

name time
AAA 2011-4-25 8:43:04
BBB 2011-4-25 9:43:10
CCC 2011-4-25 9:50:24
AAA 2011-4-25 11:50:44


各位帮帮忙!


...全文
101 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
kobaer 2011-04-29
  • 打赏
  • 举报
回复
对不起各位
让你们伤脑筋了


我发现这样设计的程序有点不合理
而且这样的程序会让很麻烦

我决定用其他方法来解决这问题

散分了
谢谢各位
AcHerat 元老 2011-04-29
  • 打赏
  • 举报
回复

select *
from tb t
where not exists (select 1 from tb where [name] = t.[name] --姓名相同
and convert(varchar(13),time,120) = convert(varchar(13),t.time,120) --时相等
and time < t.time --取时间较小的!
)
cd731107 2011-04-29
  • 打赏
  • 举报
回复
把id改了一下,重发

id name time
1 AAA 2011-4-25 8:43:04
2 AAA 2011-4-25 9:15:09
3 BBB 2011-4-25 9:43:10
4 AAA 2011-4-25 9:43:01
5 AAA 2011-4-25 10:25:01
6 AAA 2011-4-25 10:55:01
7 BBB 2011-4-25 9:49:22
8 CCC 2011-4-25 9:50:24
9 AAA 2011-4-25 11:50:44
cd731107 2011-04-29
  • 打赏
  • 举报
回复
感觉逻辑有点问题,如何判断一个小时以内,比如下面数据,我加了几条
id name time
1 AAA 2011-4-25 8:43:04
2 AAA 2011-4-25 9:15:09
3 BBB 2011-4-25 9:43:10
4 AAA 2011-4-25 9:43:01
5 AAA 2011-4-25 10:25:01
5 AAA 2011-4-25 10:55:01
5 BBB 2011-4-25 9:49:22
6 CCC 2011-4-25 9:50:24
7 AAA 2011-4-25 11:50:44
Shawn 2011-04-29
  • 打赏
  • 举报
回复
今天太激动了
SELECT a.* 
FROM tb a
INNER JOIN
(
SELECT [name],date=CONVERT(CHAR(13), GETDATE(), 120), id=MIN(id)
FROM tb
GROUP BY [name],CONVERT(CHAR(13), GETDATE(), 120)
) b
ON a.id = b.id
快溜 2011-04-29
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 kobaer 的回复:]
引用 5 楼 acherat 的回复:
引用 3 楼 kobaer 的回复:

好快 我试试


用4楼的,没注意到按小时来算!


有问题

我数据库中有 25 26 28 等时间的数据

只能查询出25的???
[/Quote]6,7楼可以试试
Shawn 2011-04-29
  • 打赏
  • 举报
回复
SELECT a.* 
FROM tb a
INNER JOIN
(
SELECT date=CONVERT(CHAR(13), GETDATE(), 120), id=MIN(id)
FROM tb
GROUP BY CONVERT(CHAR(13), GETDATE(), 120)
) b
ON a.id = b.id
AcHerat 元老 2011-04-29
  • 打赏
  • 举报
回复
select *
from tb t
where not exists (select 1 from tb where [name] = t.[name] and convert(varchar(13),time,120) < convert(varchar(13),t.time,120))


不好意思,取到分了。
kobaer 2011-04-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acherat 的回复:]
引用 3 楼 kobaer 的回复:

好快 我试试


用4楼的,没注意到按小时来算!
[/Quote]

有问题

我数据库中有 25 26 28 等时间的数据

只能查询出25的???
cd731107 2011-04-29
  • 打赏
  • 举报
回复
估计要用游标了
daishaodong 2011-04-29
  • 打赏
  • 举报
回复
利用自比较的原理。。
Shawn 2011-04-29
  • 打赏
  • 举报
回复
SELECT a.* 
FROM tb a
INNER JOIN
(
SELECT [name], date=CONVERT(CHAR(10), [time], 120), hour=DATEPART(HOUR, [time]), id=MIN(id)
FROM tb
GROUP BY [name], CONVERT(CHAR(10), [time], 120),DATEPART(HOUR, [time])
) b
ON a.id = b.id
yy1987316 2011-04-29
  • 打赏
  • 举报
回复
看来看去 怎么都没看到一小时啊
快溜 2011-04-29
  • 打赏
  • 举报
回复
select *
from tb t
where time = (select min(time) from tb where [name] = t.[name]
and convert(varchar(13,[time],120))=convert(varchar(13,t.[time],120)))
快溜 2011-04-29
  • 打赏
  • 举报
回复
select *
from tb t
where not exists (select 1 from tb where [name] = t.[name]
and convert(varchar(13,[time],120))=convert(varchar(13,t.[time],120)) and time < t.time)
AcHerat 元老 2011-04-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 kobaer 的回复:]

好快 我试试
[/Quote]

用4楼的,没注意到按小时来算!
AcHerat 元老 2011-04-29
  • 打赏
  • 举报
回复

select *
from tb t
where not exists (select 1 from tb where [name] = t.[name] and convert(varchar(16),time,120) < convert(varchar(16),t.time,120))
kobaer 2011-04-29
  • 打赏
  • 举报
回复
好快 我试试
AcHerat 元老 2011-04-29
  • 打赏
  • 举报
回复

select *
from tb t
where time = (select min(time) from tb where [name] = t.[name])
AcHerat 元老 2011-04-29
  • 打赏
  • 举报
回复

select *
from tb t
where not exists (select 1 from tb where [name] = t.[name] and time < t.time)

34,597

社区成员

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

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