Need help with a SQL Server query

Status
Not open for further replies.

SeismoJones

Ars Tribunus Militum
2,822
Hey guys, I'm having a helluva time getting a SQL query to pull back the data I need.
The boss wants to know what the max CPU utilization is on any single app server for every hour of the day for a given date.
So, the query should bring back the date/time and maxvalue for 00 (midnight), 01, 02, 03, etc., and return only 24 rows, each one showing the max CPU utilization (the maxvalue column).
There are 8 app servers. This query brings back the entries every hour, but for all 8 app servers...

Code:
select distinct dateadd(s,c.ARCHIVEDTIME/1000,'19700101') as DateAndTime
	,c.resid
	,c.maxvalue
	from CPU_MinMaxAvgData c
	inner join ManagedObject m on m.resourceid = c.resid
	where c.resid in (select resourceid from ManagedObject where displayname like 'app_' and type='Linux')
	and dateadd(s,c.ARCHIVEDTIME/1000,'19700101') between '2015-07-08 00:00:00.000' 
  and '2015-07-08 23:01:00.000'

And the results are...
Code:
DateAndTime	        resid	 maxvalue
2015-07-08 00:00:00.000	80000818	2
2015-07-08 00:00:00.000	80000831	1
2015-07-08 00:00:00.000	80000844	2
2015-07-08 00:00:00.000	80000857	2
2015-07-08 00:00:00.000	80000870	2
2015-07-08 00:00:00.000	80000883	2
2015-07-08 00:00:00.000	80000896	2
2015-07-08 00:00:00.000	80000909	2
2015-07-08 01:00:00.000	80000818	2
2015-07-08 01:00:00.000	80000831	2
2015-07-08 01:00:00.000	80000844	2
2015-07-08 01:00:00.000	80000857	2
2015-07-08 01:00:00.000	80000870	2
2015-07-08 01:00:00.000	80000883	2
2015-07-08 01:00:00.000	80000896	2
2015-07-08 01:00:00.000	80000909	2
2015-07-08 02:00:00.000	80000818	2
2015-07-08 02:00:00.000	80000831	2
2015-07-08 02:00:00.000	80000844	3
2015-07-08 02:00:00.000	80000857	2
2015-07-08 02:00:00.000	80000870	2
2015-07-08 02:00:00.000	80000883	2
2015-07-08 02:00:00.000	80000896	2
I clipped off hours 3am through 11pm - you get the idea.
So for the midnight hour the max is 2, 1am it's 2, 2am it's 3.
So I'd like to get results like so...
Code:
2015-07-08 00:00:00.000	80000818	2
2015-07-08 01:00:00.000	80000909	2
2015-07-08 02:00:00.000	80000844	3

The resid column is a reference to a server name in the ManagedObjects table.
I don't need that in the results but just selecting it out of curiosity.

I've been googling and querying all day long and haven't made any progress.
Every GROUP BY clause and HAVING clause I've tried, I've had nothing but errors.

Any ideas?
 

SeismoJones

Ars Tribunus Militum
2,822
Holy crap I think I just got it.
Finally hit this site via google.
I have literally spent about 6 hours on this today.
Jeez. looks like I needed to put max(c.maxvalue) in the select (2nd line).

Code:
  select dateadd(s,c.ARCHIVEDTIME/1000,'19700101') as DateAndTime
	,max(c.maxvalue) as 'Utilization'
	from CPU_MinMaxAvgData c
	inner join ManagedObject m on m.resourceid = c.resid
	where c.resid in (select resourceid from ManagedObject where displayname like 'app_' and type='Linux')
	and dateadd(s,c.ARCHIVEDTIME/1000,'19700101') between '2015-07-08 00:00:00.000' 
  and '2015-07-08 23:01:00.000'
  group by dateadd(s,c.ARCHIVEDTIME/1000,'19700101')

DateAndTime	Utilization
2015-07-08 00:00:00.000	2
2015-07-08 01:00:00.000	2
2015-07-08 02:00:00.000	3
2015-07-08 03:00:00.000	3
2015-07-08 04:00:00.000	4
2015-07-08 05:00:00.000	2
2015-07-08 06:00:00.000	1
2015-07-08 07:00:00.000	1
2015-07-08 08:00:00.000	1
2015-07-08 09:00:00.000	1
2015-07-08 10:00:00.000	1
2015-07-08 11:00:00.000	1
2015-07-08 12:00:00.000	1
2015-07-08 13:00:00.000	2
2015-07-08 14:00:00.000	2
2015-07-08 15:00:00.000	3
2015-07-08 16:00:00.000	3
2015-07-08 17:00:00.000	3
2015-07-08 18:00:00.000	4
2015-07-08 19:00:00.000	3
2015-07-08 20:00:00.000	4
2015-07-08 21:00:00.000	4
2015-07-08 22:00:00.000	3
2015-07-08 23:00:00.000	3
 
Status
Not open for further replies.