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)
 SQL Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

IK1972
Starting Member

48 Posts

Posted - 12/11/2012 :  16:23:41  Show Profile  Reply with Quote

I have one table with 3 column

ID,FileName, CreatedDate
1, abc.txt, getdate()

now basically I need to create a hourly status report how many file we received in hour. (based on CreatedDate) issue is I want to display '0' if for any hour we didn't received any request.

so result should be like this. 24 columns as one column for every hour and first column is for date.

---------------------- Day Hours -----------------
Date, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, .... 20, 21, 22, 23

Thanks


sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/11/2012 :  21:27:29  Show Profile  Reply with Quote
Select CreatedDate,[1],[2],[3],[4],[5],[6],[7],,[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24] 
from
(
Select ID,FileName,CreatedDate,DATEPART(hour,CreatedDate)Hourly
from Table
)P
PIVOT 
( COUNT(FileName) for Hourly in ([1],[2],[3],[4],[5],[6],[7],,[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24])) as HourlyFilecount
Go to Top of Page

IK1972
Starting Member

48 Posts

Posted - 12/12/2012 :  08:19:52  Show Profile  Reply with Quote
Thanks sodeep,

Only thing is now some dates are missing like spouse if I don't have any count for 12/10/2012 then this date will not display but I want it should be on daily basis regardless of counts. If there is no counts then date still should display with all hours are 0.

Any help really appreciated.

Thanks

Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/12/2012 :  12:41:22  Show Profile  Reply with Quote
Why don't you put all the dates in that table and based on that you can use it.
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.3 seconds. Powered By: Snitz Forums 2000