I have a SQL query that looks like this:
SELECT TOP 1000 FROM [Mydb].[dbo].[Table1] AS t1
LEFT JOIN (
SELECT fk_id, Email FROM dbo.Table2
) AS t2 ON t1.id = t2.fk_id
But this gives me the error:
Invalid object name 'dbo.Table2'.
Any idea why SQL Server does not recognize Table2 in my subquery?
PS.
I tried to rename dbo.Table2 to [Mydb].[dbo].[Table2]. But that gives me the same error.
First of all, Your query formation isn't correct. no need of that subquery at all.
Your posted query
SELECT TOP 1000 FROM [Mydb].[dbo].[Table1] AS t1
LEFT JOIN (
SELECT fk_id, Email FROM dbo.Table2
) AS t2 ON t1.id = t2.fk_id
Can be simplified as below, give it a try
SELECT TOP 1000 * FROM [Table1] t1
LEFT JOIN Table2 t2 ON t1.id = t2.fk_id
Not very sure if this is the problem but you have missed the asterisk (*) after top 1000. So maybe it should be something like the following
SELECT TOP 1000 * FROM [Mydb].[dbo].[Table1] AS t1
LEFT JOIN (
SELECT fk_id, Email FROM dbo.Table2
) AS t2 ON t1.id = t2.fk_id
If this doesnt work, remove dbo from dbo.Table2 and then try.
Hope this helps.
Related
I'm working on a BigQuery tutorial with the ncaa dataset. Any idea what I might be doing wrong here? Not sure why t1 isnt recognised. The error I get is: 'Table name "t1" missing dataset while no default dataset is set in the request'
SELECT
t1.name,
t2.name,
t1.venue_state,
t2.venue_state
FROM (
SELECT
*
FROM
`bigquery-public-data.ncaa_basketball.mbb_teams`
JOIN
`bigquery-public-data.ncaa_basketball.team_colors` id) AS t1
CROSS JOIN
t1 AS t2
WHERE
t1.venue_state=t2.venue_state
AND t1.color=t2.color
AND t1.market!=t2.market
ORDER BY
t1.name ASC
LIMIT
10
If I duplicate the nested select it works but its not pretty.
SELECT
t1.name,
t2.name,
t1.venue_state,
t2.venue_state,
t1.color,
t2.color
FROM (
SELECT
*
FROM
`bigquery-public-data.ncaa_basketball.mbb_teams` AS T
JOIN
`bigquery-public-data.ncaa_basketball.team_colors` AS C ON C.id=T.id) AS t1
CROSS JOIN
(
SELECT
*
FROM
`bigquery-public-data.ncaa_basketball.mbb_teams` AS T
JOIN
`bigquery-public-data.ncaa_basketball.team_colors` AS C ON C.id=T.id) AS t2
WHERE
t1.venue_state=t2.venue_state AND t1.color=t2.color AND t1.name<>t2.name
ORDER BY
t1.name ASC
LIMIT
10
Any explanation or help would be brilliant.
I'm trying to make a query at work in which I count all occurrences of an id in one table and then I link the description and the colors associated with those ids from another.
But I seem to have botched up my SQL Syntax (it's been a while, sorry!). It's probably just a silly mistake and I'd be so greatful to whoever could help me out!
SELECT
t1.activity_status_id,
count(*),
t2.description,
t2.color
FROM
dbo.Activity t1
INNER JOIN (
dbo.Activity_Status t2 ON t1.activity_status_id = t2.id)
GROUP BY
activity_status_id
Just remove unnecessary brackets ( and ) around inner join:
SELECT ....
FROM
dbo.Activity t1
INNER JOIN dbo.Activity_Status t2 ON t1.activity_status_id = t2.id
GROUP BY ....
SELECT t1.activity_status_id,
Count(*),
t2.description,
t2.color
FROM dbo.Activity t1
INNER JOIN dbo.Activity_Status t2
ON t1.activity_status_id = t2.id
GROUP BY t1.activity_status_id ,t2.description,t2.color
I have this little query that is working fine:
SELECT * FROM Components AS T1
WHERE Cost=null or Cost=''
Order by Code
Now I need to grab also the filed "description" from other tables where code=code
SELECT * FROM Components AS T1
WHERE Cost=null or Cost=''
LEFT JOIN on Table_321 AS T2
where T1.Code=T2.Code
Order by Code
But it is giving me a sintax error around "LEFT" which I have not been able to solve and I am not sure if such JOIN is the correct way to get it. Some help indicating me the how to solve the problem will be really appreciated. Moreover, I have also another table "Table_621 from which I need to take the description. How can I add this second table in the query?
SELECT * FROM Components T1
LEFT JOIN Table_321 T2 ON T1.Code=T2.Code
LEFT JOIN Table3 T3 ON T3.Code = T1.Code
WHERE T1.Cost=null or T1.Cost=''
ORDER BY T1.Code
Order by is ambiguous In below case:
SELECT * FROM Components T1
LEFT JOIN Table_321 T2
ON T1.Code=T2.Code
WHERE T1.Cost=null or T1.Cost=''
ORDER BY Code
try like this
SELECT * FROM Components T1
LEFT JOIN Table_321 T2
ON T1.Code=T2.Code
WHERE T1.Cost=null or T1.Cost=''
ORDER BY T2.Code --or T1.Code
SELECT * FROM Components AS T1
LEFT JOIN Table_321 AS T2
ON T1.Code=T2.Code
WHERE Cost=null or Cost=''
Order by Code
I have two queries I have created in Access and I'm a step away from arriving at the data I need. I need to subset in some way.
Table1 and Table2 (Actually query1 and query2). They both have 3 fields: Email, Matcher and List.
I need to get all the results from Table2 where Email does not exist in Table1.
I found some posts about using an outer join and where null clause. I could not get it to work though. Didn't post what I tried here in case I was off course.
select t2.*
from table2 t2
left join table1 t1 on t2.email = t1.email
where t1.email is null
SELECT t2.*
FROM table2 t2
WHERE NOT EXISTS (
SELECT *
FROM table1 t1
WHERE t1.email = t2.email
)
I'm using Oracle 10, but the best way to ask this question is with an example.
select *
from t1, t2
where t1.id = t2.id
and t1.otherID = (select max(otherID)
from t2
where id = THE ID FROM THE OUTER QUERY T1
)
I think you see where I'm trying to go with this. I need to reference t1 in the subquery to join it to the max of t2.
I need to know how to create a query like this.
"THE ID FROM THE OUTER QUERY T1" is where my confusion is.
I tried using t1.id, but did not get results.
Try the following
select t1.*, t2.*
from t1
join t2 on t1.id = t2.id
join (select id, max(otherID) as max_otherID
from t2
group by id
) a ON a.id = t1.id and a.max_otherID = t1.otherID
Using a sub-query on the join often gives better performance than using it in the where clause.