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.
| Author |
Topic |
|
zanthony
Starting Member
3 Posts |
Posted - 2005-06-03 : 09:55:08
|
| I'm trying desperately to figure out how to achieve the following - any help would be much appreciated.ProductID Basic Intermediate Total Products1 3 2 52 1 0 13 5 3 8That is, a count of how many of each product are tagged as 'basic' and 'intermediate', plus the total number of products that exist regardless. (Table structure and a query are below.) I can get the total products, I can get a count of how many of each product are 'basic' or 'intermediate', but I can't seem to put it all together. Here is a rough idea of what I want:------------------------SELECT ProductID, COUNT(SELECT ID FROM Levels WHERE ID = 1 AND ProductID = 345) AS Basic, COUNT(SELECT ID FROM Levels WHERE ID = 2 AND ProductID = 345) AS Intermediate, COUNT(SELECT ID FROM Levels WHERE ID IN (1,2) AND ProductID = 345) AS TotalCountFROM ProductsWHERE ProductID = 345------------------------But it seems I can't do a count on a subquery. What I'd really like is to get a count of the number of 'hits' for each of the nested subqueries.Table structureThe ServiceLevel of each product is linked to it via NCContracts, hence the joins. I'm trying to get this all into one query because I'd like to avoid running two simpler queries multiple times - this will be used to build a long list of products.Thanks in advance!Zach |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-06-03 : 10:18:49
|
| See if this worksSELECT Distinct ProductID,(SELECT count(ID) FROM Levels WHERE ID = 1 AND ProductID = 345) AS Basic, (SELECT count(ID) FROM Levels WHERE ID = 2 AND ProductID = 345) AS Intermediate, (SELECT count(ID) FROM Levels WHERE ID IN (1,2) AND ProductID = 345) AS TotalCountFROM ProductsWHEREProductID = 345MadhivananFailing to plan is Planning to fail |
 |
|
|
zanthony
Starting Member
3 Posts |
Posted - 2005-06-03 : 10:33:19
|
Hi Madhivanan,Thanks for the help. The layout of the results looks right, but its still not quite working. In all cases the count for Basic and Intermediate is the same. Eg:ProductID Basic Intermediate TotalCount1 1 1 22 1 1 23 1 1 2Even though in actuality only product 3 is both 'basic' and 'intermediate'.Any ideas?quote: Originally posted by madhivanan See if this worksSELECT Distinct ProductID,(SELECT count(ID) FROM Levels WHERE ID = 1 AND ProductID = 345) AS Basic, (SELECT count(ID) FROM Levels WHERE ID = 2 AND ProductID = 345) AS Intermediate, (SELECT count(ID) FROM Levels WHERE ID IN (1,2) AND ProductID = 345) AS TotalCountFROM ProductsWHEREProductID = 345MadhivananFailing to plan is Planning to fail
|
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-06-03 : 11:30:00
|
| to conditionally count a row, use:SUM(CASE WHEN condition THEN 1 ELSE 0 END) as ConditionalCountDon't use COUNT...- Jeff |
 |
|
|
zanthony
Starting Member
3 Posts |
Posted - 2005-06-03 : 13:45:29
|
BRILLIANT! Thanks for the help Jeff, I've got it working now.quote: Originally posted by jsmith8858 to conditionally count a row, use:SUM(CASE WHEN condition THEN 1 ELSE 0 END) as ConditionalCountDon't use COUNT...- Jeff
|
 |
|
|
|
|
|
|
|