Solved

Inner Join with nested select?

Posted on 2006-07-22
10
1,175 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

830 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