473,796 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Group Query and want to include null values

sks
Hi,

I have a table which logs for sake of argument page hits:

id, timestamp, ipaddress

Now, I'm running a query to get an activity report and I want to group
together all the hits from a certain time period. Let's say a month.

I can do this with this query:

select count(*), FROM_UNIXTIME(t imestamp,\"%u%y \") as d from tablename group
by d

My question is, if there are no rows for a certain time period, then the
query just misses that out. So I might get a result set something like this:

100, Jan
104, Feb
56, Apr

But I want the query to include all months, or all days, or whatever the
period is I'm querying for even if the count is zero. I want the above to be

100, Jan
104, Feb
0, Mar
56, Apr
Jul 20 '05 #1
1 2618
"sks" <sk************ ******@tek-removeme-spam-catcherheads.co .uk> wrote in
message
select count(*), FROM_UNIXTIME(t imestamp,\"%u%y \") as d from tablename group by d

My question is, if there are no rows for a certain time period, then the
query just misses that out. So I might get a result set something like this:
100, Jan
104, Feb
56, Apr
I don't think GROUP BY lets you do this because it doesn't know about
records which don't exist. If you have a table of possible time periods
(like the year + month), then you can left outer join to your activities
table 'd' above. Your aggregation function should not be count(*), but
maybe sum(case ...), or maybe something else if you can find it.

But I want the query to include all months, or all days, or whatever the
period is I'm querying for even if the count is zero. I want the above to be
100, Jan
104, Feb
0, Mar
56, Apr


Jul 20 '05 #2

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

Similar topics

3
4618
by: Rich Protzel | last post by:
Hello, So my table contains say 100,000 records, and I need to group the categories in fld1 by the highest count of subcategories. Say fld1 contains categories A, B, C, D, E. All of these categories contain subcategories AA, AB, AC, AD,...AJ, BA, BB...BJ, CA, CB, CC...CJ, etc in fld2. I am counting how many subcategories are listed for each category. Like
2
1414
by: Laphan | last post by:
Hi All Sorry for the spare info. It wasn't because I didn't want to display it. I just didn't know how to get the data that you needed. I think I've got it now so please note the following: 1) The DB is SQL 6.5. 2) My DDL for the tables in question:
9
3309
by: cyrus.kapadia | last post by:
Let's say I have the following table: entry product quality 1 A 80 2 A 70 3 A 80 4 B 60 5 B 90 6 C 80 7 D 80
2
4449
by: KoliPoki | last post by:
Greetings. I'm having a little trouble with a query. The idea is simple I need to display a list of recently unique visited URLs and the last time I visited. I have 2 table one stores the user and time/date and another has the URL. So all I need is the URL (no duplicates) and the last visit.
3
2064
by: brendan_gallagher_2001 | last post by:
Hi, I have a view(A) and I am trying to do a join on another table (B) to include only rows where date values in view A is greater than in table B. I also want the view to pick up rows in viewA based on date values. Here is what I have so far: SELECT * FROM viewA vw left JOIN tableB tb ON
5
3233
by: Mike Nolan | last post by:
I notice that 7.4 doesn't do default ordering on a 'group by', so you have to throw in an 'order by' clause to get the output in ascending group order. Is this something that most RDB's have historically done (including PG prior to 7.4) but isn't really part of the SQL standard? On a mostly unrelated topic, does the SQL standard indicate whether NULL should sort to the front or the back? Is there a way to force it to one or the other...
2
9254
by: M | last post by:
If I have a query I am writing, I can use the top 10 function to bring back the top 10 rows. That's all fine if all I want is 10 rows. What if I have a grouped query, and I have 5 entities that I want 10 rows each from? ie, I have 5 buildings I would want max 50 rows. I tried Union, which works if you want them all, but I would like the user to be able to supply me a list of buildings and be able to bring back 10 rows for each...
10
10004
by: Rudolf Bargholz | last post by:
Perhaps some kind soul could help me out with an SQL I have been trying all day to get to work, where one colum is just not summing up the way I want it to. I have the following data GRP_SEQ ITEM_SEQ NR_ITEMS PERSONS_SEQ 1 A 2 aa 1 A 2 bb
1
3985
by: Rantoun | last post by:
I know what I am asking for is pretty hard, but still maybe there is someone out there who is able to solve my problem! I have a table that look like this CREATE TABLE VEHICLETRIPS ( VID NUMBER(10) NOT NULL, VECHIELID VARCHAR2(10) NOT NULL, DEVICEDATE DATE NOT NULL, TRIPID NUMBER(10) NOT NULL, COMMUNITY VARCHAR2(10) NOT NULL
0
9529
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9054
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7550
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.