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

SQL WHERE CLAUSE HELP

I need a little help with an assignment....
Basically I have a table with several PRODUCTS
PRODUCT(P_ID, P_Name, P_Class, P_Price, Product_Supplier)

Anyways....

I need to display:

List each Product Class, number of items in the class and the average
Price of the items in the class. List only those classes that have more
than three (3) items.

I have this

Select P_Class, COUNT(P_Class), AVG(P_Price)
>From PRODUCT
Group by P_Class;

Here are the results

P_CLASS COUNT(P_CLASS) AVG(P_Price)
------------ -------------- ------------
Camera 2 362.975
Printer 4 255.2375
DVD 2 83.75
Processor 3 139.95
Storage 8 391.855
Software 2 322.5
OS 2 292.495
Monitor 3 184.65

But I'm not for sure how to get the WHERE in there correctly to just
show me the items with the 3 or more COUNT (items) in them. Any help
would be great.

Thanks

Oct 11 '06 #1
2 2345
RickyZane wrote:
I need a little help with an assignment....
Basically I have a table with several PRODUCTS
PRODUCT(P_ID, P_Name, P_Class, P_Price, Product_Supplier)

Anyways....

I need to display:

List each Product Class, number of items in the class and the average
Price of the items in the class. List only those classes that have more
than three (3) items.

I have this

Select P_Class, COUNT(P_Class), AVG(P_Price)
>>From PRODUCT
Group by P_Class;

Here are the results

P_CLASS COUNT(P_CLASS) AVG(P_Price)
------------ -------------- ------------
Camera 2 362.975
Printer 4 255.2375
DVD 2 83.75
Processor 3 139.95
Storage 8 391.855
Software 2 322.5
OS 2 292.495
Monitor 3 184.65

But I'm not for sure how to get the WHERE in there correctly to just
show me the items with the 3 or more COUNT (items) in them. Any help
would be great.

Thanks
What you need to use is the HAVING clause. WHERE operates on values
*before* any aggregates are applies, so any count(*) isn't yet
available. HAVING works on values *after* the aggregates. Example:

Select P_Class, COUNT(P_Class), AVG(P_Price)
From PRODUCT
Group by P_Class
HAVING COUNT(P_Class) >= 3
Oct 11 '06 #2
Adam Ruth wrote:
RickyZane wrote:
>I need a little help with an assignment....

Select P_Class, COUNT(P_Class), AVG(P_Price)
From PRODUCT
Group by P_Class
HAVING COUNT(P_Class) >= 3
That's not assistance ... that is the answer. Please help
instructors help students by providing hints ... not answers.

A sufficient answer would have been: "Consider using the HAVING clause."

Thank you.
--
Daniel A. Morgan
University of Washington
da******@x.washington.edu
(replace x with u to respond)
Oct 12 '06 #3

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

Similar topics

5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
11
by: Summa | last post by:
Hi NG, I have the following problem that I hope you can help me with (MS-SQL server 2000) Imagine a statement like this: "select id, firstname, (select top 1 id from testdata) as testid,...
2
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20...
1
by: mailar | last post by:
Hi, Can I use ORDER BY clause in my sql UDF that returns a table and has the folloing body. (trial_udf.sql) drop function aa@ create function aa() returns TABLE(empno varchar(20)) language...
3
by: Razzie | last post by:
Hi, I know that as a general rule, whenever your class contains members that implicitly or explicitly implement IDisposable, your class should too. However, does it count when my class uses a...
5
by: No bother | last post by:
I am using 5.0.26-NT on Windows 2000. I have need to use a reference in the outer from clause in a subquery in the select clause. Consider the following example: Select (select b.baitID from...
9
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ...
8
by: chrisdavis | last post by:
I'm trying to filter by query or put those values in a distinct query in a where clause in some sort of list that it goes through but NOT at the same time. Example: ROW1 ROW2 ROW3 ROW4 ,...
3
by: Rahul Babbar | last post by:
Hi, I had the following doubts about the "For Read Only" clause. 1. How does a "for Read only" clause improve the performance? 2. How does a "for Read only" clause compare with "With UR"...
3
by: bilbo | last post by:
Can anybody help me understand why I get the error "Syntax error in CONSTRAINT clause"? I get it in Access 2003 and Access 2007. Both are clean installs with no add-ins Running this code in...
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
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
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...

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.