SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


return top 5 per datepart


return top 5 per datepart

Author
Message
gchappell
gchappell
SSC-Enthusiastic
SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)

Group: General Forum Members
Points: 141 Visits: 219
i am wanting to return the top 5 errors by hour. the following query returns all

select count(ERROR_LOG_NO) as ErrorCount, error_name, DatePart(Hour,ERROR_LOG_DATE) as ErrorHour, ERROR_SEVERITY
from my_table
where error_severity < 4
group by DatePart(Hour,ERROR_LOG_DATE), error_name, Error_Severity
order by ErrorHour desc, ErrorCount desc

the following returns the top 5 form the latest hour

select top(5)count(ERROR_LOG_NO) as ErrorCount, error_name, DatePart(Hour,ERROR_LOG_DATE) as ErrorHour, ERROR_SEVERITY
from my_table
where error_severity < 4
group by DatePart(Hour,ERROR_LOG_DATE), error_name, Error_Severity
order by ErrorHour desc, ErrorCount desc

how do I combine the two queries to return the top5 for each datepart?
Fraggle-805517
Fraggle-805517
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2429 Visits: 1512
gchappell (7/12/2013)
i am wanting to return the top 5 errors by hour. the following query returns all

select count(ERROR_LOG_NO) as ErrorCount, error_name, DatePart(Hour,ERROR_LOG_DATE) as ErrorHour, ERROR_SEVERITY
from my_table
where error_severity < 4
group by DatePart(Hour,ERROR_LOG_DATE), error_name, Error_Severity
order by ErrorHour desc, ErrorCount desc

the following returns the top 5 form the latest hour

select top(5)count(ERROR_LOG_NO) as ErrorCount, error_name, DatePart(Hour,ERROR_LOG_DATE) as ErrorHour, ERROR_SEVERITY
from my_table
where error_severity < 4
group by DatePart(Hour,ERROR_LOG_DATE), error_name, Error_Severity
order by ErrorHour desc, ErrorCount desc

how do I combine the two queries to return the top5 for each datepart?


Your going to want to do a subquery using the row_number() over (partition by DATEPART() ORDER BY ErrorHour desc, errorcount desc). This will provide you with the numbers. Then in the outer query, just state where the row_number column <= 5

so something like this:


SELECT *
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY DATEPART(HOUR, ERROR_LOG_DATE) ORDER BY DATEPART(HOUR, Error_log_Date)) AS ID
, Count(Error_log_No) OVER (Partition by DATEPART(HOUR, Error_log_date)) ErrorCount
, Error_Severity
from table
where error_severity < 4
)
WHERE ID <= 5



Performance note, if you expect this table to grow, I would suggest adding a new column that stores the DatePart so you aren't having to use the DATEPART function each query, which would kill performance on large tables.

Fraggle
gchappell
gchappell
SSC-Enthusiastic
SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)

Group: General Forum Members
Points: 141 Visits: 219
Thanks Fraggle i'm going to try it now
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search