Author |
Topic |
rett
Starting Member
35 Posts |
Posted - 2007-12-14 : 18:21:34
|
Here we go.I have categories and Name ID's. Within a category I would like to know the total number of Name ID's for each categoryexample:Accumula 105294Aerosols 105058Air Cond 105294Air Star 105058Bottomho 109967Cement-P 109967Consulta 211252Drill Co 211252Safety-E 105294Here my statementSELECT DISTINCT L.LAST_CATEGORY_DESC, COUNT(L.NAME_ID) AS Expr1FROM dbo.Table AS L INNER JOIN dbo.CatTable AS C ON L.PUB_ID = C.PUB_ID AND L.LAST_CATEGORY_LVL_ID = C.LAST_CATEGORY_LVL_IDWHERE (C.WEB_ACTIVATED_IND = 1) AND GROUP BY L.LAST_CATEGORY_DESC |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-12-15 : 10:03:21
|
1) Why do you have DISTINCT in your SELECT? That should not be there, there is no reason for it. Be sure that you completely understand how DISTINCT works and how GROUP BY works.2) What is your question or problem?- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
rett
Starting Member
35 Posts |
Posted - 2007-12-15 : 11:44:56
|
Hi Jeff, Here's my problem. In my table I have categories that can appear multiple times. So I want to only show the category description 1 time but also get the total number of Name ID's that are under that 1 category.By using the DISTINCT it is giving me the 1 category but the COUNT is not working for the total number of Name ID'srett |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2007-12-15 : 12:04:59
|
When you GROUP BY category It will not appear multiple times in the output. So take DISTINCT out of select statement to get distinct category names and count of associated name ids.Alternatively if you are having repeated Category and Name id groups then take DISTINCT of these and then group by Category and take count. |
 |
|
rett
Starting Member
35 Posts |
Posted - 2007-12-15 : 12:33:51
|
Okay, Remove the DISTINCT and using the GROUP BY is working. But when I use the COUNT(L.NAME_ID). I kept getting the number 2 for each.For example: I should be getting 3 Name ID's for category "Consulta" but I get the number 2 for all categories.Accumula 105294Aerosols 105058Air Cond 105294 105292Air Star 105058Consulta 211252 109967 211252Drill Co 211252Safety-E 105294 |
 |
|
cardgunner
326 Posts |
Posted - 2007-12-15 : 13:24:55
|
rett if I may ask to understand. From the info on the first post the result you are looking for is 3 1052942 1050582 1099672 211252??Card Gunner |
 |
|
rett
Starting Member
35 Posts |
Posted - 2007-12-15 : 13:33:47
|
hi cardgunner, The results that I am looking for is:Total number of Name ID's for category "Consulta" 3Total number of Name ID's for category "Air Cond" 2Total number of Name ID's for category "Drill Co" 1etc...I just want the COUNT of the Name ID's but not display the Name ID'sThanks |
 |
|
cardgunner
326 Posts |
Posted - 2007-12-15 : 13:44:20
|
Given your example on the first post that is not accurate.Accumula 105294Aerosols 105058Air Cond 105294Air Star 105058Bottomho 109967Cement-P 109967Consulta 211252Drill Co 211252Safety-E 105294Card Gunner |
 |
|
rett
Starting Member
35 Posts |
Posted - 2007-12-15 : 14:10:16
|
hi cardgunner,From my first example each category should only have a total number of 1:Accumula 1 Name Id (105294) for this categoryAerosols 1 Name Id (105058) for this categoryAir Cond 1 Name Id (105294) for this categoryAir Star 1 Name Id (105058) for this categoryBottomho 1 Name Id (109967) for this categoryCement-P 1 Name Id (109967) for this categoryConsulta 1 Name Id (211252) for this categoryDrill Co 1 Name Id (211252) for this categorySafety-E 1 Name Id (105294) for this category |
 |
|
cardgunner
326 Posts |
Posted - 2007-12-15 : 14:21:26
|
rett, from the results from post 7Total number of Name ID's for category "Consulta" 3Total number of Name ID's for category "Air Cond" 2Total number of Name ID's for category "Drill Co" 1would be in your underlying table or query without the count function asconsulta 211252air cond 105294consulta 111111drill co 211252consulta 222222air cond 333333where consulta is in your table with 3 different id's and so and so on.If that is correct, if it was in there twice with the same ID should we count it as 2 or 1? I would guess 1 but..Card Gunner |
 |
|
rett
Starting Member
35 Posts |
Posted - 2007-12-15 : 15:19:07
|
hi cardgunner, yes it is possible to have the same Name ID more then 1 time. so the answer would be 2 not 1.thanks |
 |
|
|