| Author |
Topic |
|
jbezanson
Starting Member
35 Posts |
Posted - 2005-07-27 : 10:49:18
|
I have 3 tables: Catalog_Content, Catalog_Groups, and Catalog_ItemI want to display the number of Groups and Items for each Catalog in Catalog_Content.There is only one Catalog in Catalog_Content: Cat_NameA=Products, Cat_ID=1.When I run my query it is giving 30 Groups and 30 Items. But there are only 10 Groups and 3 Items. It is multiplying them together for some reason and I can't figure out why. I have tested with other tables and columns and it IS multiplying them.Here is my query:SELECT Cat_NameA, c.Cat_ID, count(g.Cat_Group_ID) Groups, count(Cat_Item_ID) ItemsFROM Catalog_Groups g, Catalog_Content c, Catalog_Item iWHERE c.Cat_ID = '1'GROUP BY Cat_NameA, c.Cat_ID |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-07-27 : 10:53:34
|
| You need to relate theree tables by keyfieldPost the table structuresMadhivananFailing to plan is Planning to fail |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-07-27 : 11:30:53
|
quote: Originally posted by madhivanan You need to relate theree tables by keyfieldPost the table structuresMadhivananFailing to plan is Planning to fail
That was a bit meagre, the poster is probably a SQL beginner, don't You think ?!.hint #2Look for "INNER JOIN" in BOL.rockmoose |
 |
|
|
jbezanson
Starting Member
35 Posts |
Posted - 2005-07-27 : 11:56:04
|
| Catalog_Content--------------------Cat_ID(PK) varchar 10Cat_NameA varchar 250Catalog_Groups--------------------Cat_Group_ID(PK) varchar 10Cat_ID varchar 10Group_LabelA varchar 100Catalog_Item-------------------------Cat_Item_ID(PK) varchar 10Cat_ID varchar 10Item_NameA varchar 250All 3 tables are 'joined', related by Cat_ID(FK)Results I want(should be)-----------------Catalog Name | Catalog Id | # of Groups | # of ItemsProducts 1 10 3Results I am getting (wrong)-----------------Catalog Name | Catalog Id | # of Groups | # of ItemsProducts 1 30 30 |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-07-27 : 12:01:42
|
| >> All 3 tables are 'joined', related by Cat_ID(FK).Yes, but You have to specify those relations in the SQL query.( see INNER JOIN in BOL )You listed the tables, but did not relate them, You are effectively doing a cross join.SELECT Cat_NameA, c.Cat_ID, count(g.Cat_Group_ID) Groups, count(Cat_Item_ID) ItemsFROMCatalog_Groups gINNER JOIN Catalog_Content c ON g.<relatedcolumns> = c.<relatedcolumns>INNER JOIN Catalog_Item i ON c.<relatedcolumns> = i.<relatedcolumns>WHERE c.Cat_ID = '1'GROUP BY Cat_NameA, c.Cat_IDok ??rockmoose |
 |
|
|
jbezanson
Starting Member
35 Posts |
Posted - 2005-07-27 : 12:09:24
|
| So here is what I have now:[CODE]SELECT Cat_NameA, c.Cat_ID, count(g.Cat_Group_ID) Groups, count(Cat_Item_ID) ItemsFROM Catalog_Groups gINNER JOIN Catalog_Content c ON g.Cat_ID = c.Cat_IDINNER JOIN Catalog_Item i ON c.Cat_ID = i.Cat_IDWHERE c.Cat_ID = '1'GROUP BY Cat_NameA, c.Cat_ID[/CODE]But I am still getting the same results.Why are they multiplying? I am using the Query Analyzer on SQL Server 2000Windows Server 2003 EnterpriseDual Xeon CPUs2 Gb DDRAsp.Net 1.1SQL Server 2000Visual Studio.Net 2003 |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-07-27 : 12:19:02
|
| SELECT Cat_NameA, c.Cat_ID, count(DISTINCT g.Cat_Group_ID) Groups, count(Cat_Item_ID) ItemsRun the query without the GROUP BY first, so that You can see the data it is grouping,it might help You understand what is going on.rockmoose |
 |
|
|
jbezanson
Starting Member
35 Posts |
Posted - 2005-07-27 : 12:21:45
|
| Adding DISTINCT worked. I tried removing the GROUP BY and got an error:Server: Msg 8118, Level 16, State 1, Line 1Column 'c.Cat_NameA' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.Server: Msg 8118, Level 16, State 1, Line 1Column 'c.Cat_ID' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.Thanks for your help.CheersWindows Server 2003 EnterpriseDual Xeon CPUs2 Gb DDRAsp.Net 1.1SQL Server 2000Visual Studio.Net 2003 |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-07-27 : 12:25:07
|
| Forgot to mention, that if You remove the group by, you can't have the aggregates sum, count ...without the group by it's just:SELECT Cat_NameA, c.Cat_ID, g.Cat_Group_ID, Cat_Item_IDrockmoose |
 |
|
|
jbezanson
Starting Member
35 Posts |
Posted - 2005-07-27 : 12:31:46
|
| Thank youI have a greater understanding now!Thanks again.Windows Server 2003 EnterpriseDual Xeon CPUs2 Gb DDRAsp.Net 1.1SQL Server 2000Visual Studio.Net 2003 |
 |
|
|
|