473,418 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Return result set for every hour of the day

Sl1ver
196 100+
Hi im writing a sql query that needs to be grouped the hours in the day e.g
if its 10 am then it should have 10 different groups with data where the date and hour corresponds to that

i've tried
Expand|Select|Wrap|Line Numbers
  1. datepart(hour, getdate())
  2.  
but it does not return the required result, any help?
Mar 5 '10 #1
2 6426
Echidna
53
Hi,

I would suggest setting up a table with the hours in the day 1 - 24 (or am/pm) or something similar.
then creating a view from this as a left outer join to the table, the datepart(hour,getdate()) function can then be used to limit the number of records produced.

so something like this could work.

Expand|Select|Wrap|Line Numbers
  1. SELECT timename, eventname from
  2. (
  3. SELECT timename, eventname from tbltime LEFT OUTER JOIN tblevent on tbltime.timeid = Datepart(hour,tblevent.datetimefield)
  4. GROUP BY (tbltime.timename, Datepart(hour,tblevent.datetimefield))
  5. HAVING (Datepart(hour,tblevent.datetimefield) <= datepart(hour,getdate())
  6. )
  7.  
it needs something to refer against.

Hope this helps

Cheers

Leon
Mar 5 '10 #2
ck9663
2,878 Expert 2GB
Here's a sub-query. You can make this a view, a table variable, a derived table or a CTE and use it to join to your table.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. select colorder as hour_of_day
  4. from
  5.    (select colorder
  6.    from syscolumns c1 
  7.       inner join (select top 1 c2.id from syscolumns c2 group by c2.id having count(*) >= 24) c3 on c1.id = c3.id
  8.    where colorder <= 24) hours_in_day 
  9.  
  10.  
OR a UNION ALL

Expand|Select|Wrap|Line Numbers
  1. select * 
  2. from (
  3.    select 1 as hour_of_day
  4.    union all
  5.    select 2
  6.    union all
  7.    select 3
  8.    union all
  9.    select 4
  10.    union all
  11.    select 5
  12.    union all
  13.    select 6
  14.    union all
  15.    select 7
  16.    union all
  17.    select 8
  18.    union all
  19.    select 9
  20.    union all
  21.    select 10
  22.    union all
  23.    select 11
  24.    union all
  25.    select 12) hours_in_day
  26.  
Happy Coding!!!

~~ CK
Mar 5 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Pan Yang Bin | last post by:
Hi, I using the following script to output 10 o'clock, the first two return corrent result, and i also check the year, month, day, hour, minute, second function they all return same result. but...
2
by: Rhino | last post by:
I am trying to verify that I correctly understand something I saw in the DB2 Information Center. I am running DB2 Personal Edition V8.2.1 on Windows. I came across the following in the Info...
5
by: Brooks Clayton | last post by:
I have an A97 db on Win2k machine. I have a make table query to update a small db wich we then FTP to our website for customer order status. What is the best way to automate the query to run...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
7
by: Bernard Lebel | last post by:
Hello, I'm stumbled at a serious problem, and quite frankly getting desparate. This is a rather long-winded one so I'll try to get straight to the point. I have this Python program, that...
3
by: Shawn Yates | last post by:
I have a database that I am using as a Time clock where employees can clock in and out. Is there a way to make it so that when they clock out a form would open displaying their work day hour by...
8
by: WakeBdr | last post by:
I'm writing a class that will query a database for some data and return the result to the caller. I need to be able to return the result of the query in several different ways: list, xml,...
18
by: terminator(jam) | last post by:
consider: struct memory_pig{//a really large type: memory_pig(){ std::cout<<"mem pig default\n"; //etc... }; memory_pig(memory_pig const&){
11
by: Peter Holschbach | last post by:
Hi, I've the following line of code: result = DateTime.ParseExact("1999-12-01T23:59:59Z", "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture); where I get in result "result" "02.12.1999...
6
by: exander77 | last post by:
I am quite new to c++, about half of a year. I juct wonder, why such code is not possible: int chlen(char * ar) { for(int i=0;ar||return i;i++); } In my opinion, it would be much "cuter"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.