Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 having

Author  Topic 

d confz
Starting Member

12 Posts

Posted - 2008-02-09 : 23:16:48
Hello..

Using JSP and SQL, I'm trying to group the names of customers with color 'red' from the table customer and sum up the quantity that they have bought using the code:
SELECT customer_name,color, sum(qty) AS qty FROM customer GROUP BY customer_name HAVING color='red'

When I tried checking the result, nothing came out. I checked the database but there are people who has the color red.. Thus, I tried to group it with color blue using the same code and suprisingly, the result I expected came out. I already checked the spelling of "red" both in the database and the code and it is correct in lowercase. What do you think is the problem with this?

Thanks a lot!!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-10 : 00:06:58
Try using WHERE & see.

SELECT customer_name,color, sum(qty) AS qty 
FROM customer
WHERE color='red'
GROUP BY customer_name


by the way i wonder how you got this statement working. How you managed to include color in SELECT list? I believe it should be written like this

SELECT c.customer_name,c.color,t.qty 
FROM customer c
INNER JOIN (SELECT customer_name,sum(qty) AS qty
FROM customer
GROUP BY customer_name )t
ON t.customer_name=c.customer_name
WHERE c.color='red'


i think difference between HAVING & WHERE is that HAVING clause will apply filteration only after performing grouping of data while WHERE clause does filteration before grouping.

see this article

[url]http://www.devx.com/DevX/Tip/21295 [/url]
Go to Top of Page

d confz
Starting Member

12 Posts

Posted - 2008-02-10 : 00:28:41
Thanks a lot.. It works using where.. :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-10 : 01:04:34
You are welcome :)
Go to Top of Page
   

- Advertisement -