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 |
|
JBelthoff
Posting Yak Master
173 Posts |
Posted - 2002-12-15 : 18:49:15
|
Hi,I'm having trouble figuring out how I do the following select statment to retrieve a recordset that includes a counted total from another table with one statement.Here is what I want to do.SELECT CatID, CatPID, CatName, (SELECT COUNT(*) FROM IPG_LISTINGS WHERE LCatPID = @@IDENTITY) As TotalFROM IPG_CATEGORIES WHERE CatPID IS NullORDER BY CatIDI would like the total of the Count(*) to be inserted into the recordset as another field ascociated with the CatID of each row. I need to count how many CatID's are located in the other table that match the row of the recordset.I hope I explained this right. I would certainly love to hear suggestions.ThanksJB |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-12-15 : 20:05:47
|
| Hi JBI'm guessing you want something like:select a.CatID, a.CatPID, a.CatName, count(b.lCatPID) as Totalfrom IPG_CATEGORIES a left join IPG_LISTINGS b on a.CatID = b.CatIDgroup by a.CatID, a.CatPID, a.CatNameorder by a.CatID, a.CatPID, a.CatNamebut can't really be sure until you post some table definitions...(don't understand why you haveWHERE LCatPID = @@IDENTITYORWHERE LCatPID IS NULL)??--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
JBelthoff
Posting Yak Master
173 Posts |
Posted - 2002-12-15 : 20:43:13
|
Hi,Maybe this will explain.This is exactly what I want to do, however there is several selects and 10 updates.CREATE TABLE #Directory ( CatID int IDENTITY, CatPID int, CatName nvarchar(100), ListTotal int)INSERT INTO #Directory SELECT CatPID, CatName, (0) FROM IPG_CATEGORIES WHERE CatPID IS NULLDECLARE @Counter intSET @Counter = 1WHILE @Counter < 11 BEGIN UPDATE #Directory SET ListTotal = (SELECT COUNT(*) FROM IPG_LISTINGS WHERE LCatPID = @Counter) WHERE CatID = @Counter SET @Counter = @Counter + 1 ENDSELECT *FROM #DirectoryDROP TABLE #DirectoryHere are My TablesIPG CATEGORIESCatID CatPID CatName ----------- ----------- ------------------------ 1 NULL Production Companies2 NULL Pre-Production3 NULL Crew4 NULL Location and Travel5 NULL Stages, Studios and Sets6 NULL Camera and Sound Equipment7 NULL Grip and Lighting Equipment8 NULL Props and Wardrobe9 NULL Post Production10 NULL Agencies11 10 Advertising Agencies12 1 Animation Companies13 1 Production Companies14 1 Corporate Video Companies15 1 Corporate Event Companies16 10 Director's Agencies17 1 Production Services18 10 Public Relations19 1 Record Companies..................... ETC......................58 4 Airlines59 4 AirportsIPG_LISTINGSLID LCatID LCatPID LAccountID ----------- ----------- ----------- ----------- 38 12 1 239 13 1 240 14 1 241 18 10 1666642 19 2 1666643 12 1 16666Here is the results set that I want. Selecting all of the IPG_CATEGORIES with CatPID as Null and then the ascociated total of those "subcategories" listed under ListTotal. Is it basically a list of all of the top categories with the total users who are listed in all of the ascociated subcategories. CatPID stands for Category Parent ID. And If that is Null that means that that is the highest on the directory you can go.CatID CatPID CatName ListTotal ----------- ----------- ------------------------------ ----------- 1 NULL Production Companies 42 NULL Pre-Production 13 NULL Crew 04 NULL Location and Travel 05 NULL Stages, Studios and Sets 06 NULL Camera and Sound Equipment 07 NULL Grip and Lighting Equipment 08 NULL Props and Wardrobe 09 NULL Post Production 010 NULL Agencies 1I hope this explains. Sometimes trying to explain is harder than the SQL! I tried your approach and got the following error. Server: Msg 207, Level 16, State 3, Line 1Invalid column name 'CatID'.JB Edited by - JBelthoff on 12/15/2002 20:49:27 |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-12-15 : 22:31:30
|
The error was because in table IPG_LISTINGS - the field is LCatID not CatID - I assumed they were the same as you saidquote: another field ascociated with the CatID of each row
So does that mean you've worked it out? Sorry - I can't tell whether you still have aquestion or not...--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
JBelthoff
Posting Yak Master
173 Posts |
Posted - 2002-12-15 : 23:57:20
|
Hi rrb,I have it so it is doing what I want now. However as you can see from the above procedure there are many steps in the process.1. Create Temp Table -- OK2. Populate it with the basic info via INSERT/SELECT -- OK3. Update the rows individually with 10 update calls in a "WHILE" loop -- ???4. SELECT the final info from the temp table. -- OKI would like to make step 3 more efficient if that is at all possible. That's the part of the query that I was trying to slim down and also trying to incorporate into step 2 doing away with step 3 completely.I hope that makes sense, it getting pretty late here in Connecticut!If you have any suggestions I would love to hear them.Thanks,JB |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-12-16 : 00:12:47
|
| aaahhhhh! Your join is a.CatID = b.LCatPIDOK - here it is:select a.catid, a.catPID, a.catname, count(b.LID)from IPG_CATEGORIES a left join IPG_LISTINGS b on a.Catid = b.LCatPIDwhere a.catPID is nullgroup by a.catid, a.catPID, a.catnameorder by a.catid, a.catPID, a.catnameCheers--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
JBelthoff
Posting Yak Master
173 Posts |
Posted - 2002-12-16 : 10:20:25
|
Hi Rob,That works great.Is there anyway to add an additional column to the result set so that it calculates how many subcategories there are for each of the listed categories?If you could lead me in the direction to figure that out myself I would appreciate it. Of course, you can also show me the code but I would rather try to figure it out for myself and then if I am still stuck I can ask again.Would I do an additional right Join back on IPG_CATEGORIES? Just a thought.Thanks,JB |
 |
|
|
JBelthoff
Posting Yak Master
173 Posts |
Posted - 2002-12-16 : 13:52:52
|
Hi Again,OK, here it is.This statement gives me the result set containing what I need.SELECT a.CatID, a.catPID, a.catname, COUNT(b.LID) ListTotalFROM IPG_CATEGORIES a LEFT JOIN IPG_LISTINGS b on a.Catid = b.LCatPIDWHERE a.catPID IS NULL GROUP BY a.catid, a.catPID, a.catname ORDER BY a.catidThis statement gives me a second result set on the subcategory countSELECT Count(c.CatPID) As SubCatTotalFROM IPG_CATEGORIES aLeft join IPG_CATEGORIES c on a.CatID = c.CatPIDWHERE a.CatPID IS NULLGROUP BY a.CatID, a.CatPID, a.CatNAmeORDER BY a.CatIDI've tried to combine them into one SELECT with no luck.Is there a way to combine these 2 statements into one and get the results I need. Which are...CatID catPID catname ListTotal SubCatCount----------- ----------- ----------------------------- ----------- -----------1 NULL Production Companies 4 72 NULL Pre-Production 1 133 NULL Crew 0 214 NULL Location and Travel 0 35 NULL Stages, Studios and Sets 0 06 NULL Camera and Sound Equipment 0 07 NULL Grip and Lighting Equipment 0 08 NULL Props and Wardrobe 0 09 NULL Post Production 0 010 NULL Agencies 1 5Thanks,JB |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-12-16 : 18:34:38
|
| Hi J - glad to hear it went OK.To be honest, there probably is some sneaky way - and maybe one of the gurus will see the light - but I'm too lazy to be smart...if it was me, I'd just take your two queries and make them subqueries, then I'd join them to sync them up.I've got the code if you want it.PS When I ran the subcategory code, I gotSubCatTotal ----------6000000003These are different to your results - is this because you have extra data in your table? Or have I missed something?Cheers--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
JBelthoff
Posting Yak Master
173 Posts |
Posted - 2002-12-16 : 19:21:07
|
| Hi Rob,Are you saying that I should create two derived tables and then do a select joining the two derived tables?And yes, you results are different because I did not list all of my data.JB |
 |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2002-12-16 : 19:23:36
|
| CREATE TABLE #IPG_CATEGORIES(CatID int,CatPID int,CatName nvarchar(50))CREATE TABLE #IPG_LISTINGS(LID int,LCatID int,LCatPID int,LAccountID int)INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (1,NULL,'Production Companies')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (2,NULL,'Pre-Production')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (3,NULL,'Crew')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (4,NULL,'Location and Travel')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (5,NULL,'Stages, Studios and Sets')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (6,NULL,'Camera and Sound Equipment')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (7,NULL,'Grip and Lighting Equipment')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (8,NULL,'Props and Wardrobe')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (9,NULL,'Post Production')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (10,NULL,'Agencies')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (11,10,'Advertising Agencies')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (12,1,'Animation Companies')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (13,1,'Production Companies')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (14,1,'Corporate Video Companies')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (15,1,'Corporate Event Companies')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (16,10,'Director''s Agencies')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (17,1,'Production Services')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (18,10,'Public Relations')INSERT INTO #IPG_CATEGORIES (CatID, CatPID, CatName) VALUES (19,1,'Record Companies')INSERT INTO #IPG_LISTINGS (LID, LCatID, LCatPID, LAccountID) VALUES (38,12,1,2)INSERT INTO #IPG_LISTINGS (LID, LCatID, LCatPID, LAccountID) VALUES (39,13,1,2) INSERT INTO #IPG_LISTINGS (LID, LCatID, LCatPID, LAccountID) VALUES (40,14,1,2)INSERT INTO #IPG_LISTINGS (LID, LCatID, LCatPID, LAccountID) VALUES (41,18,10,16666)INSERT INTO #IPG_LISTINGS (LID, LCatID, LCatPID, LAccountID) VALUES (42,19,2,16666) INSERT INTO #IPG_LISTINGS (LID, LCatID, LCatPID, LAccountID) VALUES (43,12,1,16666)SELECT a.CatID, a.CatName, (select count(*) from #IPG_LISTINGS b where a.CatID = b.LCatPID) ListTotal,(select count(*) from #IPG_CATEGORIES c where a.CatID = c.CatPID) SubCatCountFROM #IPG_CATEGORIES a WHERE a.CatPID Is NullDROP TABLE #IPG_CATEGORIESDROP TABLE #IPG_LISTINGS |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-12-16 : 19:26:06
|
| Thanks ValterYep JB - exactly as Valter has done. Oh well, I guess you won't be able to work it out for yourself now....--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2002-12-16 : 19:42:17
|
quote: CatID catPID catname ListTotal SubCatCount ----------- ----------- ----------------------------- ----------- ----------- 1 NULL Production Companies 4 7 2 NULL Pre-Production 1 13 3 NULL Crew 0 21 4 NULL Location and Travel 0 3 5 NULL Stages, Studios and Sets 0 0 6 NULL Camera and Sound Equipment 0 0 7 NULL Grip and Lighting Equipment 0 0 8 NULL Props and Wardrobe 0 0 9 NULL Post Production 0 0 10 NULL Agencies 1 5
My pleasure rrb but I must say you did most of the work it's a lot easier to understand what people want when they give you expected results. I read this post when it was in it's infancy and I just put it off for later due to question but you stuck it out and deserve all the credit.I hope everyone is listening and from now on posts nice ddl, sample data and expected results. |
 |
|
|
JBelthoff
Posting Yak Master
173 Posts |
Posted - 2002-12-16 : 20:25:00
|
Hi Again,Thanks Rob and Valter.I must say it seems easy when you guys do it. I'll post the expected results sooner next time. Until than, I'll be pluggin away.JB |
 |
|
|
|
|
|
|
|