Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a sql table having three columns: date, time, and value. Values are in any time and there are many values during the same minute. I would need to get out just values for each minute, and those should be an average of all values during the same minute.

If the data in the table is:
SQL
myDate		myTime		myValue
2012.01.12	10:51:33	100
2012.01.12	10:51:45	200
2012.01.12	10:52:28	300
2012.01.12	10:52:41	200
2012.01.12	10:52:58	100


the output should be something like:
SQL
myDate		myTime		myValue
2012.01.12	10:51:00	150
2012.01.12	10:52:00	200


Any proposals of what kind of sql would result in this?
Thanks for any help.
Posted
Comments
Om Prakash Pant 13-Jan-12 7:44am    
what are the datatypes of each column?

Try this

SQL
if object_id('tempdb..#temp') is not null
	begin
	drop table #temp
	end

create table #temp
	(
	myDate varchar(10),
	myTime varchar(8),
	myValue int
	)

insert into #temp values('2012.01.12',	'10:51:33',	100)
insert into #temp values('2012.01.12',	'10:51:45',	200)
insert into #temp values('2012.01.12',	'10:52:28',	300)
insert into #temp values('2012.01.12',	'10:52:41',	200)
insert into #temp values('2012.01.12',	'10:52:58',	100)

select	myDate,
	myTime,
	avg(myValue) as myValue
from	(
	select	myDate,
		left(myTime,5)+':00' as myTime,
		myValue
	from	#temp
	) a
group
by	myDate,
	myTime
 
Share this answer
 
v2
Comments
Petri Luoto 13-Jan-12 14:55pm    
Processing with real data 780.000 lines resulting in 268.000 lines took 11 seconds! Great! Also the solution is simple and the reulting table is clear with original data types:
2012-01-06 09:01 3581
2012-01-06 09:00 3222
2012-01-06 08:59 3268
2012-01-06 08:58 3314
2012-01-06 08:57 3254

I will use this solution. Thanks!
Try this

SQL
DECLARE @timedata as table (mydate datetime , mytime datetime , myvalues bigint)
DECLARE @timedata_Formated as table (mydate datetime , mytime NVARCHAR(255) , myvalues bigint)

insert into @timedata select '2012.01.12','10:51:33.000',100
insert into @timedata select '2012.01.12','10:51:45.000',200
insert into @timedata select '2012.01.12','10:52:28.000',300
insert into @timedata select '2012.01.12','10:52:41.000',200
insert into @timedata select '2012.01.12','10:52:58.000',100

INSERT INTO @timedata_Formated
SELECT mydate,CAST(DATEPART(hh,mytime) AS NVARCHAR(50)) + ':' + CAST(DATEPART(mi,mytime) AS NVARCHAR(50)) + ':00',myvalues   FROM @timedata

SELECT mydate,mytime,AVG(myvalues) FROM @timedata_Formated Group by mydate,mytime
 
Share this answer
 
Comments
Petri Luoto 13-Jan-12 14:49pm    
Thanks! Processing of 780.000 lines resulting in 268.000 lines took 14 seconds. Quite nice. But there was a problem: the time column went funny after 59 minutes:

2012-01-06 00:00:00.000 9:1:00 3581
2012-01-06 00:00:00.000 9:0:00 3222
2012-01-06 00:00:00.000 8:9:00 1116
2012-01-06 00:00:00.000 8:8:00 944
2012-01-06 00:00:00.000 8:7:00 960
2012-01-06 00:00:00.000 8:6:00 960
2012-01-06 00:00:00.000 8:59:00 3268
2012-01-06 00:00:00.000 8:58:00 3314
2012-01-06 00:00:00.000 8:57:00 3254
this may not be the cleaner & optimum way but you can acheive this by following query:
SQL
select myDate, left(myTime,2) + ':' + substring(myTime, 4,2) + ':00' , sum(cast(right(myTime,2) as int)), avg(myValue) from table_data group by myDate, left(myTime,2), substring(myTime, 4,2) 
 
Share this answer
 
Comments
Petri Luoto 13-Jan-12 14:30pm    
Thanks! I tried this, but for some reason got an error message: "Argument data type time is invalid for argument 1 of substring function."
I did not investigate it closer, but perhaps something with datatypes. Those were Date, Time(7), and int.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900