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

Help with SELECT DISTINCT

122 100+
I have a table that counts food boxes received by each household (hhId).

Table Name: food_box
Fields: fbId (PK), fbDate, hhId (FK)

Currently, the table holds the following data:

fbId,Date,hhId
1,1 /1 /2009,1
2,2 /1 /2009,1
3,3 /1 /2009,1
4,4 /1 /2009,1
5,5 /1 /2009,1
6,6 /1 /2009,1
7,12/1 /2008,1
8,7 /1 /2009,1
9,1 /15/2009,1
10,2 /15/2009,1
11,3 /15/2009,1
17,7 /5 /2009,1
18,4 /15/2009,1
19,7 /7 /2009,2
20,7 /7 /2009,2
21,7 /7 /2009,2

I want a query that gives me a count of the unduplicated households that received a food box (answer should be 2).

It won’t let me do
Expand|Select|Wrap|Line Numbers
  1. SELECT count(DISTINCT hhId) FROM food_box;
but
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT count(hhId) FROM food_box;
gives me a count of 15.

I don't know why this is so difficult for me. Must be lunch time. Thanks in advance for any help.
Jul 7 '09 #1
5 2752
Annalyzer:

Does this give you what you are looking for:

Expand|Select|Wrap|Line Numbers
  1. select count(hhid) from food_box group by hhid
sphinney

@Annalyzer
Jul 7 '09 #2
Annalyzer
122 100+
Ohhhhhhhhhhhhh, of course! Boy do I feel dumb. I was trying to make it harder than it really was. I had the DISTINCT keyword stuck in my brain when I didn't even need it.

Thanks so much! Back on track now.
Jul 7 '09 #3
Annalyzer
122 100+
Oops! Wait a second. That gives me the total number of food boxes for each year. I need the unduplicated count of households for each year.

Anybody?
Jul 7 '09 #4
Annalyzer:

Oops. My bad. I should have tested my suggestion before making it. Try the following. I've tested this and it DOES work!

Expand|Select|Wrap|Line Numbers
  1. SELECT Count([Sub_query].hhId) AS Count_Of_Households
  2. FROM (select hhId from food_box group by hhId)  AS [Sub_query]
What you're trying to do is really a two step process. First you're trying to find the unique household ID's (hhid). This is accomplished with a subquery. Second, you're wanting to count them.

Regards,
sphinney

@Annalyzer
Jul 8 '09 #5
Annalyzer
122 100+
That explains why I was able to do it with 2 queries. I like you're solution much better, though, thank you. It's much neater.
Jul 9 '09 #6

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

Similar topics

4
by: Surendra | last post by:
I have this query that I need to use in an Update statement to populate a field in the table by the value of Sq ---------------------------------------------------------------------------- Inline...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
3
by: Andrew | last post by:
Hi, I have a problem I would really appreciate help with. I am generating dynamic SQL and need to optimise it. The specific example I am trying to optimise looks like this: SELECT DISTINCT...
3
by: phillip.s.powell | last post by:
Is this how it's done? INSERT INTO student ( (SELECT DISTINCT a.* FROM student_spring a, student_summer b WHERE a.unique_key != b.unique_key)\ UNION (SELECT DISTINCT b.* FROM student_summer...
0
by: hahahardididi | last post by:
Hi Forums, I have a frustrating problem on my Stored Procedure. It can only proccess about 100 records in 10 minutes. I have 2 million initial records that need to processed. Meaning that with...
2
by: Matt Bob | last post by:
Thanks in advance for any help... I have a single table: Employees: Name DOB startdate dept ---------|----------|-------------|------------ Al 1/1/1940 4/5/2003 ...
10
by: Bob Bedford | last post by:
Hi all, I've ever the same problem. Table1 idperson, name, zip table2 zip, city, region.
10
by: mscurto | last post by:
What is the syntax for an sql command to get the following. I want to pull in a handful of fields from a table but one of the fields needs to be unique. For example, if I have a customer table...
1
by: Webstorm | last post by:
Hi, I hope someone can help me sort this out a bit, Im completely lost. Here is the page I am working on: http://www.knzbusinessbrokers.com/default.asp I have 3 search critera that I need to...
0
by: davidsavill | last post by:
Hi All, I am migrating a database from Firebird/Interbase to DB2 and have having issues with the stored procedures/functions. I have a number of functions that loop over a FOR loop, each pass...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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,...

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.