Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Inner Join with nested select?

Posted on 2006-07-22
10
Medium Priority
?
1,185 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
  • 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 800 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
Independent Software Vendors: 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!

 
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 1200 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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

885 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