Solved

Inner Join with nested select?

Posted on 2006-07-22
10
1,178 Views
Last Modified: 2012-06-21
Here is my sql that works:

Select Tag_ID, Count(*) as TheCount from (tbn_Tags t INNER JOIN tbn_Files m ON t.Media_ID=m.Sha1 ) Where (Ext = 'mp3' OR Ext = 'wav' ) Group By Tag_ID ORDER BY TheCount desc,Tag_ID

Now i have another table called tbn_TagClicks and i want to inner join it on the Tag_ID column that i selected above with tbn_TagClicks.ChildTag column and also get the tbn_TagClicks.Clicks column selected as well

not sure what to write, i tried a few things and they failed.

so i tried soemthing liek this :

Select Tag_ID,TheCount,Clicks from (Select Tag_ID, Count(*) as TheCount from (tbn_Tags t INNER JOIN tbn_Files m ON t.Media_ID=m.Sha1 ) Where (Ext = 'mp3' OR Ext = 'wav' )   )    Inner join tbn_TagClicks On Tag_ID=ChildTag  Group By Tag_ID ORDER BY TheCount desc,Tag_ID

but it failed
0
Comment
Question by:joshuadavidlee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17160496

Select Tag_ID,TheCount,Clicks from
(
Select Tag_ID, Count(*) as TheCount
from tbn_Tags t
INNER JOIN tbn_Files m ON t.Media_ID=m.Sha1
Where (Ext = 'mp3' OR Ext = 'wav' )  
) A    ---------- u missed the alias here
Inner join tbn_TagClicks On A.Tag_ID=ChildTag  
Group By Tag_ID
ORDER BY TheCount desc,Tag_ID
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 200 total points
ID: 17160499
Select Tag_ID,TheCount,Clicks from (
   Select Tag_ID, Count(*) as TheCount
   from (tbn_Tags t INNER JOIN tbn_Files m ON t.Media_ID=m.Sha1 )
   Where (Ext = 'mp3' OR Ext = 'wav' )  
   ) as l    -- << this is important
Inner join tbn_TagClicks On Tag_ID=ChildTag  
Group By Tag_ID
ORDER BY TheCount desc,Tag_ID


note: you should give all the tables used in the query a short alias name, and prefix all the column with the alias:


Select l.Tag_ID, l.TheCount, tc.Clicks from (
   Select t.Tag_ID, Count(*) as TheCount
   from (tbn_Tags t INNER JOIN tbn_Files m ON t.Media_ID=m.Sha1 )
   Where (m.Ext = 'mp3' OR m.Ext = 'wav' )  
   ) as l    -- << this is important
Inner join tbn_TagClicks tc On l.Tag_ID=tc.ChildTag  
Group By l.Tag_ID
ORDER BY l.TheCount desc, l.Tag_ID

0
 

Author Comment

by:joshuadavidlee
ID: 17160515
i also forgot to mention there was a column in tbn_TagClicks called TagPath that i wanted to filter by also but let me look at whats been said so far
0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17160533
Select Tag_ID,TheCount,Clicks from
(
Select Tag_ID, Count(*) as TheCount
from tbn_Tags t
INNER JOIN tbn_Files m ON t.Media_ID=m.Sha1
Where (Ext = 'mp3' OR Ext = 'wav' )  
) A    ---------- u missed the alias here
Inner join tbn_TagClicks On A.Tag_ID=tbn_TagClicks.ChildTag  
WHERE tbn_TagClicks.TagPath = 'urFilterValue'
Group By Tag_ID
ORDER BY TheCount desc,Tag_ID
0
 

Author Comment

by:joshuadavidlee
ID: 17160575
Column 'tbn_Tags.Tag_ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

thats my error for :

Select Tag_ID,TheCount,Clicks from ( Select Tag_ID, Count(*) as TheCount from (tbn_Tags t INNER JOIN tbn_Files m ON t.Media_ID=m.Sha1 ) Where (Ext = 'mp3' OR Ext = 'wav' ) ) as g Inner Join tbn_TagClicks On g.Tag_Id=ChildTag Group By Tag_ID ORDER BY TheCount desc,Tag_ID
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17160587
you missed the " GROUP BY " statemnet

Select Tag_ID,TheCount,Clicks from ( Select Tag_ID, Count(*) as TheCount from (tbn_Tags t INNER JOIN tbn_Files m ON t.Media_ID=m.Sha1 ) Where (Ext = 'mp3' OR Ext = 'wav' ) GROUP BY Tag_ID) as g Inner Join tbn_TagClicks On g.Tag_Id=ChildTag Group By Tag_ID ORDER BY TheCount desc,Tag_ID
0
 

Author Comment

by:joshuadavidlee
ID: 17160596
dont know why i need that but i tried it and got same error
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 300 total points
ID: 17160602
Select Tag_ID,TheCount,Clicks from
( Select Tag_ID, Count(*) as TheCount from
 tbn_Tags t
 INNER JOIN tbn_Files m ON t.Media_ID=m.Sha1
 Where (Ext = 'mp3' OR Ext = 'wav')  
GROUP BY Tag_ID) as g
Inner Join tbn_TagClicks On g.Tag_Id=ChildTag
ORDER BY TheCount desc,Tag_ID
0
 

Author Comment

by:joshuadavidlee
ID: 17160631
ok so i must have a typeo somewhere cause i copied and pasted yours and it worked. So i have one more small question

if there is no tag_id to join to childtag then there is no row in the result for that combo, but would i change inner join to join or maybe outer join if i wanted a row in the result for the tag_id even if there was no childtag to match it up with?

I have more rows in my tbn_Tags table(100) then i do the tbn_TagClicks table(20) but i want 100 rows in my result.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17160637
replace the inner join with a LEFT OUTER JOIN
> Inner Join tbn_TagClicks On g.Tag_Id=ChildTag

LEFT OUTER JOIN tbn_TagClicks On g.Tag_Id=ChildTag
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question