all groups > sql server programming > january 2006 >
You're in the

sql server programming

group:

Query help


Query help Lisa Calla
1/30/2006 10:55:04 PM
sql server programming: Hi,
I have a table with productIDs, productShapes, and productColors columns.
I'd like a query that results in:

productShape A Count(products regardless of color) Count(Blue
products)
productShape B Count(products regardless of color) Count(Blue
products)
productShape ... Count(products regardless of color) Count(Blue
products)

Is it necessary to use a nested select statement? My sql experience has
been somewhat limited, though now I've upgraded to SQL Server 2005 Express.
TIA for any help.


Re: Query help Jay Sanderson
1/31/2006 12:00:00 AM
If you want the result set returned like that then nested selects would be
the easiest way to go as you want two counts in the same result row. if you
had only one count you could use GROUP BY.


[quoted text, click to view]

Re: Query help Hugo Kornelis
1/31/2006 11:59:35 PM
[quoted text, click to view]

Hi Lisa,

Not at all. In fact, it's far more efficient to use a simple query with
a CASE expression:

SELECT ProductShape,
COUNT(*) AS CountOfAllColours,
COUNT(CASE WHEN ProductColor = 'Blue'
THEN 'Count Me'
END) AS CountOfOnlyBlueProducts
FROM YourTable
GROUP BY ProductShape

(untested - see www.aspfaq.com/5006 if you prefer a tested reply)

--
Re: Query help Lisa Calla
2/1/2006 7:03:00 PM
Thanks very much!, I ended up with something very similar.


[quoted text, click to view]

AddThis Social Bookmark Button