日期:2014-05-18  浏览次数:20531 次

求一sql语句,取得每分钟的数据
一张表,a_Time时间类型 2012-1-13 14:48:14
我现在要取得当前一小时的每分钟的记录条数

该怎么写?select m1,m2,m3.........m60 ???
这样写不可能吧?

谁能写一个?




------解决方案--------------------
datepart和case when行列转换吧
------解决方案--------------------
where datediff(hh,getdate(),a_Time) = 0
------解决方案--------------------
SQL code
select count(*),convert(varchar(120),a_time,120) from tb
group by convert(varchar(120),a_time,120)

------解决方案--------------------
SQL code
select sum(case when datepart(mi,a_time)=1 then 1 else 0 end) as m1
 sum(case when datepart(mi,a_time)=2 then 1 else 0 end) as m2
 sum(case when datepart(mi,a_time)=3 then 1 else 0 end) as m3
 sum(case when datepart(mi,a_time)=4 then 1 else 0 end) as m4
 sum(case when datepart(mi,a_time)=5 then 1 else 0 end) as m5
  ............................
 sum(case when datepart(mi,a_time)=59 then 1 else 0 end) as m59
 sum(case when datepart(mi,a_time)=60 then 1 else 0 end) as m60
from tb

------解决方案--------------------
探讨
SQL code
select count(*),convert(varchar(120),a_time,120) from tb
group by convert(varchar(120),a_time,120)

------解决方案--------------------
select count(*),MINUTE(SYSDATE()) from tb group by MINUTE(SYSDATE()) 这是My sql 写法
------解决方案--------------------
SQL code

declare @date datetime 
select @date = '2012-02-01 12:00:00'
select 
dateadd( mi,-number,@date ) 
from 
master.dbo.spt_values a 
where 
a.type = 'p' and 
a.number < 60

------解决方案--------------------
小三 大虾的回答应该是正确的
datediff(hour,time,getdate())=0
这就是取当前1小时内的数据的SQL 语句
------解决方案--------------------
select DATEPART (mi, datetransaction), count(*)
from
(
select datetransaction
from tabletransaction
where datetransaction <= getdate() and datetransaction >= dateadd (mi, -60 , getdate())
) z
group by DATEPART( mi, datetransaction )


------解决方案--------------------
select DATEPART (mi, datetransaction), count(*)
from
(
select datetransaction
from tabletransaction
where datetransaction <= getdate() and datetransaction >= dateadd (mi, -59 , getdate())
) z
group by DATEPART( mi, datetransaction )

-59 才对 

------解决方案--------------------
不用写60条啊 用for循环
------解决方案--------------------
不用写60条啊 用for循环
------解决方案--------------------
create table tdate
(
tid int identity(1,1) primary key,
tdate datetime
)
go

insert into tdate 
select getdate() union all 
select getdate() union all 
select getdate() union all 
select getdate() union all 
select getdate() union all 
select getdate() union all 
select getdate() union all 
select getdate() union all 
select getdate() union all 
select getdate() union all 
select getdate() union all 
select getdate()
select count(datepart(mi,ttdate)) as 次数 from tdate where datediff(hh,ttdate,'2012-5-6 8:01:33')=0 group by datepart(mi,ttdate)