SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Select first five rows every for each hour.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sergeant_time
Yak Posting Veteran

73 Posts

Posted - 10/02/2012 :  13:46:54  Show Profile  Reply with Quote
I am trying to write a query that selects the first five survey response for each hour. I would like to Order it by day and hour. Below is my query:


SELECT Q1, SurveyDate, DATEPART(hh, SurveyDate)
AS 'Hour'
FROM hrc_survey_results_2
WHERE (businessline = 4)
ORDER BY SurveyDate, 'Hour'

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 10/02/2012 :  14:56:56  Show Profile  Reply with Quote

SELECT Q1,[Hour]
FROM
(
SELECT Q1,ROW_NUMBER() OVER (PARTITION BY DATEPART(hh, SurveyDate) ORDER BY SurveyDate) AS Seq,
DATEPART(hh, SurveyDate) AS 'Hour'
FROM hrc_survey_results_2
WHERE (businessline = 4)
)t
WHERE Seq<=5
ORDER BY DATEADD(dd,DATEDIFF(dd,0,SurveyDate),0), [Hour],Seq


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


Edited by - visakh16 on 10/02/2012 14:57:14
Go to Top of Page

sergeant_time
Yak Posting Veteran

73 Posts

Posted - 10/02/2012 :  15:05:01  Show Profile  Reply with Quote
@visakh16,

Irecieved this error code when I ran it. I am using SSRS 2008

The OVER SQL construct or statement is not supported.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 10/02/2012 :  15:34:12  Show Profile  Reply with Quote
quote:
Originally posted by sergeant_time

@visakh16,

Irecieved this error code when I ran it. I am using SSRS 2008

The OVER SQL construct or statement is not supported.


where are you using this? Use it in procedure created from SSMS and call it from SSRS. So far as database is above 90 compatibility level it will work fine
Dont use it from query editor within SSRS

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000