Solved

Inner Join with nested select?

Posted on 2006-07-22
10
1,181 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

617 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