473,322 Members | 1,806 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,322 software developers and data experts.

Simple count()/group by question

Hello all,

I have a table with among other things a timestamp field. To get the
count of records per hour I do:

select
count(*) as c,
date_trunc('hour', timestmp) as h
from
mytable
where
date_trunc('day', timestmp) = date_trunc('day', now())
group by h;

Which works, but it only returns a count for hours that have entries.
I'd like it to return count 0 as well for the hours that have no entries.
In other words I'd like my result set to have 24 rows no matter what.

Any suggestions?

Thanks,

Joseph
Jul 19 '05 #1
1 7462
Joseph Santaniello wrote:
Hello all,

I have a table with among other things a timestamp field. To get the
count of records per hour I do:

select
count(*) as c,
date_trunc('hour', timestmp) as h
from
mytable
where
date_trunc('day', timestmp) = date_trunc('day', now())
group by h;

Which works, but it only returns a count for hours that have entries.
I'd like it to return count 0 as well for the hours that have no entries.
In other words I'd like my result set to have 24 rows no matter what.

Any suggestions?


I did something similar by left-joining the summary with a view that I
created that looks something like this:

CREATE VIEW "hours_in_past_24" AS
SELECT "timestamp"(date_trunc('hour',now())) as the_hour, 0 as
hours_ago
UNION SELECT "timestamp"(date_trunc('hour',now() - "interval"('1
hours'))), 1
UNION ...
SELECT "timestamp"(date_trunc('hour',now() - "interval"('23 hours'))), 23
ORDER BY 1;

Now you do something like:

select count(*) as c, date_trunc('hour', timestmp) as h
from hours_in_past_24
left join my_table on (the_hour = h)
etc.
--
Jeff Boes vox 269.226.9550 ext 24
Database Engineer fax 269.349.9076
Nexcerpt, Inc. http://www.nexcerpt.com
...Nexcerpt... Extend your Expertise

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gary W | last post by:
Hello, I am stuck on what is probably a very easy select query. The query is: Select Count(*), Distinct PID, ID, Name From Table 1 Group By PID I get:
1
by: Jim Clark | last post by:
Suppose the following tables: parent ------ parentid address phone
18
by: Geoff Cox | last post by:
Hello, I am trying to print out the array values for a second time but get error on page message? Thanks Geoff <html>
2
by: DKode | last post by:
Ok, Here is my XML File, it's pretty simple: <?xml version="1.0" encoding="utf-8" ?> <Dispositions> <Group Name="Unused"> <Disp>NA</Disp> <Disp>TCB</Disp> <Disp>WPC</Disp> </Group>
6
by: Eddie Smit | last post by:
field- field- surname town ---- ---- john NY john Vegas john Boston eddie Boston eddie New Orleans eddie NY
5
by: Cro | last post by:
Hello Access Developers, I'd like to know if it is possible to perform a count in an expression that defines a control source. My report is based on a query. In my report, I want a text box to...
15
by: Richard Hollenbeck | last post by:
For example, one college course has only 24 students in it, but the following code says there are zero As, 20 Bs, 16 Cs, 4 Ds, and 8 Fs. When it prints it then says 0 As, 40 Bs, 32 Cs, 8 Ds, and...
7
by: Riley DeWiley | last post by:
I am continually amazed by SQL's ability to humble me .... I have a toy query into a toy database that looks just like this: SELECT . FROM f, fw, w WHERE f.id = fw.fid and fw.wid = w.id and...
1
by: heckstein | last post by:
I am working in Access 2002 and trying to create a report from our company's learming management system. I am not a DBA and most of my SQL knowledge has been self taught through trial and error. I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.