Solved

Inner Join with nested select?

Posted on 2006-07-22
10
1,169 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 142

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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now