求一sql语句,有点难

qi7 2003-11-21 08:54:27
数据库中的数据按时间为序排列,为4或10分钟一个数据,现在我想生成一个结果集:每个小时一个数据,即60分钟一个数据的数据集。
...全文
70 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
txlicenhe 2003-11-24
  • 打赏
  • 举报
回复
参考这个:
http://expert.csdn.net/Expert/topic/2437/2437014.xml?temp=.67857
交流]自增号


应用举例
eg1:
create table t(日期 char(8),请假人数 int)
insert t select '20031001',3
Union all select '20031003',2
Union all select '20031004',1
Union all select '30031031',5
要列出2003年10月每一天的请假人数,若没有,以0表示。

Select convert(char(8),dateadd(day,id,'20031001'),112),IsNull(t.请假人数,0) from
(Select top 31 (select sum(1) from sysobjects where name<= a.name)-1 as id from sysobjects a) bb
left join t on convert(char(8),dateadd(day,id,'20031001'),112) = t.日期
qi7 2003-11-24
  • 打赏
  • 举报
回复
高手可否再指教:
现在单用户的数据我可以查出,我怎么能查出几个用户在一时间段中每个小时的数据的数据集。
qi7 2003-11-24
  • 打赏
  • 举报
回复
谢谢。还有个问题,解决加分。
我原来把SQL SERVER 的程序和数据都放到系统盘中了,现在想把数据放到别的盘上,有方便点的方法吗?
pengdali 2003-11-24
  • 打赏
  • 举报
回复
select * from data a where time between @starttime and @endtime
and time=(select max(time) from data where datediff(hh,time,a.time)=0 and
time between @startime and @endtime and userid=a.userid)
qi7 2003-11-24
  • 打赏
  • 举报
回复
单用户数据搜索可以做到,多用户数据数据搜索怎么做。
select * from data a where time between @starttime and @endtime and time=(select max(time) from data where datediff(hh,time,a.time)=0 and time between @startime and @endtime)
如果用户号字段为userid,按用户号分类 (group by userid)怎么做?????
sjw800614 2003-11-24
  • 打赏
  • 举报
回复
sjw800614 2003-11-24
  • 打赏
  • 举报
回复
SELECT * FROM tblName WHERE 时间列 BETWEEN (COVNERT(CONVERT(VARCHAR(10),时间列,121) AS DATETIME , dayadd(h,1,CONVERT(CONVERT(VARCHAR(10),时间列,121) as datetime)


--把时间转化成 字符而 后重新转化为时间,比较麻烦是真事!
qi7 2003-11-24
  • 打赏
  • 举报
回复
看不懂,能用简单的sql语句解决吗?
qi7 2003-11-21
  • 打赏
  • 举报
回复
比如我的表名为data,他的时间列为time,是不是下面的语句?
select * from data a where time between(@starttime and @endtime) and time=(select max(time) from data where datediff(hh,time,a.time)=0 and time between(@startime and @endtime))
pengdali 2003-11-21
  • 打赏
  • 举报
回复
select * from 表 tem where 时间列 between @你的开始时间 and @结束时间 and 时间列=(select min(时间列) from 表 where datediff(hh,时间列,tem.时间列)=0 and 时间列 between @你的开始时间 and @结束时间)

或:

select * from 表 tem where 时间列=(select max(时间列) from 表 where datediff(hh,时间列,tem.时间列)=0 and 时间列 between @你的开始时间 and @结束时间)
pengdali 2003-11-21
  • 打赏
  • 举报
回复
select * from 表 tem where 时间列 between @你的开始时间 and 结束时间 and 时间列=(select max(时间列) from 表 where datediff(hh,时间列,tem.时间列)=0 and 时间列 between @你的开始时间 and 结束时间)
lynx1111 2003-11-21
  • 打赏
  • 举报
回复
select CONVERT(varchar(13),col,120),count(*) from table13 group by CONVERT(varchar(13),col,120)


qi7 2003-11-21
  • 打赏
  • 举报
回复
说明白点,好吗?
我要的是任意时间段内的每小时一个数据的数据集。
pengdali 2003-11-21
  • 打赏
  • 举报
回复
select * from 表 where datediff(mi,列,getdate()) between 0 and 60


select CONVERT(varchar(13),列,120),count(*) from 表 group by CONVERT(varchar(13),列,120)


select CONVERT(varchar(10), getDate(),120) --不要时间2002-1-1
select convert(char(8),getdate(),112) ----20020101
select convert(char(8),getdate(),108) ---06:05:05
pengdali 2003-11-21
  • 打赏
  • 举报
回复
select datediff(day,'2001-1-1','2002-1-1') 天
select datediff(month'2001-1-1','2002-1-1') 月
select datediff(year,'2001-1-1','2002-1-1') 年
pengdali 2003-11-21
  • 打赏
  • 举报
回复
当前小时:

select * from 表 where datediff(hh,列,getdate())=0

22,210

社区成员

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

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