Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
NickyJ
Starting Member
46 Posts |
Posted - 2008-04-08 : 17:00:14
|
I have data as below and require 2 recordsets against the data.Name Time CodeA Smith 08:12 CEEA Smith 08:14 CEEA Smith 08:46 CEEA Smith 08:48 FFRA Smith 11:12 FFRNow first I need a total count for all activities for every 5 minutes starting from on the hour which I have got by the following :SELECT Time,COUNT(*) as [Count]FROM(SELECT CONVERT(CHAR(5),DATEADD(MINUTE,FLOOR(DATEDIFF(MINUTE,'20000101',time)/5.0)*5,'20000101'),108)FROM tablename) AS Time(Time)GROUP BY TimeThis gives me my totals every 5 mins as below :08:05 0 so between 08:00 and 08:04:5908:10 008:15 2 etc.....Now I need also to count and group by name and code every 5 minutes but can't get this to work,any help greatly appreciated.Need data to look like this :Name Time Code CountA Smith 08:05 NULL 0A Smith 08:10 NULL 0A Smith 08:15 CEE 2A Smith 08:20 NULL 0A Smith 08:25 NULL 0A Smith 08:30 NULL 0A Smith 08:35 NULL 0A Smith 08:40 NULL 0A Smith 08:45 NULL 0A Smith 08:50 CEE 1A Smith 08:50 FFR 1etc....Many thanks in advance |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-04-08 : 17:18:33
|
SELECT name, code, Time,COUNT(*) as [Count]FROM(SELECT CONVERT(CHAR(5),DATEADD(MINUTE,FLOOR(DATEDIFF(MINUTE,'20000101',time)/5.0)*5,'20000101'),108)FROM tablename) AS Time(Time)GROUP BY name, code, Time==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
NickyJ
Starting Member
46 Posts |
Posted - 2008-04-09 : 03:46:14
|
Thanks for response but get msg.. Server: Msg 207, Level 16, State 1, Line 9Invalid column name 'name'.Server: Msg 207, Level 16, State 1, Line 9Invalid column name 'code'.as those columns are not within derived table, when I did add them then I get Server: Msg 8158, Level 16, State 1, Line 1'Time' has more columns than were specified in the column list. |
 |
|
NickyJ
Starting Member
46 Posts |
Posted - 2008-04-09 : 04:14:41
|
Have sorted this now as follows :SELECT Time.Time,Time.Name,Time.Code,COUNT(*) as [Count]FROM(SELECT CONVERT(CHAR(5),DATEADD(MINUTE,FLOOR(DATEDIFF(MINUTE,'20000101',time)/5.0)*5,'20000101'),108) as Time, Name, CodeFROM vw_PowerHour) AS TimeGROUP BY Time, Name, CodeThanks for help... |
 |
|
|
|
|
|
|