Incorrect Syntax near the keyword On - sql

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

Related

Query with JOIN on multiple tables

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

Left join subquery gives invalid object name error

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.

Transform select count(*) inside a inner join

My problem here is that i'm modifying an existing query and i cannot use count(*) in the query.
I have to use inner join subqueries.
What i need to "transform" into my inner join is like this (this works):
SELECT count(distinct t1.id)
FROM table1 t1
WHERE t1.column1 = 'value1' AND
t2.column2 = 'value2' AND
EXISTS(select 1 from table2 t2 where t2.id = t1.id)
My global query looks like this:
SELECT [many many column]
FROM table2 t2
INNER JOIN [...]
LEFT OUTER JOIN [...]
--[I NEED MY COUNT HERE, see below for example]
WHERE [some conditions are true]
ORDER BY [some column]
What i found to help me is something like this:
SELECT [many many column], myJoin.Count
FROM table2 t2
INNER JOIN (
SELECT tt2.id, count(distinct tt2.id) as Count
FROM table2 tt2
WHERE EXISTS (SELECT 1 FROM table1 tt1 where tt1.id = tt2.id)
GROUP BY tt2.id) myJoin
on t2.id = myJoin.id;
See what i'm trying to acheive? I need to count the ids, joining 2 tables, but i can't have a count in my main query, i can't possibly copy-paste all the "group by" condition that would go with it...
I'm on sql server.
If i find the answer i will come back and post it.
Thanks for any advice/tricks about this.
How about the following:
SELECT table2.*, TopQ.MyCount
FROM (
SELECT t2.id, myJoin.MyCount
FROM table2 t2
INNER JOIN (
SELECT tt2.id, count(distinct tt2.id) as MyCount
FROM table2 tt2
WHERE EXISTS
(SELECT 1 FROM table1 tt1 where tt1.id = tt2.id)
GROUP BY tt2.id) AS myJoin
on t2.id = myJoin.id
)AS TopQ
INNER JOIN table2 ON TopQ.id = table2.id
I came across this:
select count(distinct t1.id) over (partition by t1.aColumn) as myCount,
[many many column]
from table2 t2
inner join table1 t1 on [someConditions] = value1 and
[someConditions] = value2 and
t2.id = t1.id;
I get the same results as my first select i posted in my question, and without adding a "group by" anywhere and a lot of inner join that im not that familliar with. I'm gonna stick with this solution.
Thanks!

Join query where table references itself

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.

SQL Joining three tables and using LEFT OUTER JOIN

I have three tables and two seperate SQL queries which are working correctly and I am having correct results.
If I try to join these three tables I am having null as result.
First query:
select T1.ID,T3.COMPANY
from T1,T3
where (T1.status!='CLOSED') and (T1.PRIORITY)>5 and T1.CLASSID=T3.CLASSID
Second query:
SELECT T1.ID, T2.DESCRIPTION
FROM T1
LEFT OUTER JOIN T2
ON T1.ID=T2.KEY
WHERE T1.status!='CLOSED'
AND (T2.CREATEDATE= (SELECT MAX(CREATEDATE)
FROM T2
WHERE T2.KEY=T1.ID))
I tried to join them but as result I am having null:
select T1.ID,T3.COMPANY,T2.DESCRIPTION
from T1
INNER JOIN T3 ON T1.CLASSID=T3.CLASSID
LEFT OUTER JOIN T2
ON T1.ID=T2.KEY
where (T1.status!='CLOSED') AND (T1.PRIORITY)>5
AND (T2.CREATEDATE= (SELECT MAX(CREATEDATE)
FROM T2
WHERE T2.KEY=T1.ID))
like it does not recognized last part for taking MAX value from T2 table.
What am I doing wrong? Thanks for help
Firstly, use an alias for the subquery on table T2.
T2.CREATEDATE =
(SELECT MAX(T2Alias.CREATEDATE)
FROM T2 AS T2Alias
WHERE T2Alias.KEY = T1.ID)
Secondly, consider moving this condition into the ON clause of the LEFT JOIN to table T2.
The first thing that jumps out at me is the new dependency on both T1.Priority > 5 and T2.CreateDate value being equal to the result of the inline query:
( AND (T1.PRIORITY) > 5
AND (T2.CREATEDATE =
(SELECT MAX(CREATEDATE) FROM T2 WHERE T2.KEY = T1.ID) )
Without the data it's difficult to check however this may be the issue