mysql subquery in select join - mysql

I have a problem with the following statement:
SELECT SUM(foreseen_charges.commonCharge) as required, foreseen_charges.flatsId
LEFT JOIN (SELECT deposits.flatsId FROM deposits GROUP BY flatsId) deps
ON foreseen_charges.flatsId = deps.flatsId
FROM foreseen_charges GROUP BY foreseen_charges.flatsId
And I always getting this error:
You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'LEFT
JOIN (SELECT deposits.flatsId FROM deposits GROUP BY flatsId) deps
ON f' at line 2
Could anyone help me?
Best Regards,
Cs

Put FROM before LEFT JOIN
SELECT SUM(foreseen_charges.commonCharge) as required,
foreseen_charges.flatsId
FROM foreseen_charges
LEFT JOIN (
SELECT deposits.flatsId
FROM deposits
GROUP BY flatsId
) deps ON foreseen_charges.flatsId = deps.flatsId
GROUP BY foreseen_charges.flatsId

You're doing things out of order. The general structure is:
SELECT (something) FROM table JOIN other-table ON table.var1=other-table.var2 WHERE situation
You're doing this:
SELECT (something) LEFT JOIN table ON table.var1=other-table.var2 FROM other-table
You can't join until you've declared both tables, and you can't relate other-table because you haven't declared it yet.
Unsure what the query is meant to accomplish, but this should fix your syntax error:
SELECT SUM(foreseen_charges.commonCharge) as required, foreseen_charges.flatsId
FROM foreseen_charges
LEFT JOIN (SELECT deposits.flatsId FROM deposits GROUP BY flatsId) deps
ON foreseen_charges.flatsId = deps.flatsId
GROUP BY foreseen_charges.flatsId

Related

Redshift - SQL Left Join does not work with correlated subquery and aggregated function

I am quite new to Redshift SQL and have been trying to figure out what's wrong with my syntax.
My task is to join 2 tables: question and user via left join as I want to retain all values from table question.
At the moment it throws the following error message: [Amazon](500310) Invalid operation: This type of correlated subquery pattern is not supported yet; when I use left join. On the other hand, when I change the code to join it works just fine. I suspect this is because I have an aggregated function and logical expression within my subquery that makes my left join an inner join.
But as I mentioned above, I need to retain all values from table question.
Below is my code
select
qa.user_id as user_email,
i.timestamp as session_login_time,
qa.timestamp as question_ask_time,
qa.question_id,
qa.question
from
schema1.question as qa
left join
schema1.user as i
on
qa.user_id = i.email
and
i.timestamp =
(select
max(timestamp)
from schema1.user
where
timestamp <= qa.timestamp)
where user_email <> 'tester' and user_email not like '%tester.com'
group by qa.user_id, i.timestamp, qa.timestamp, qa.question_id, qa.question
The purpose of the subquery is to get the closest session_login_time to each of the question_ask_time. So, multiple rows of question can have the same session_login_time value.
Could anybody please help me identify what I am missing from my code above? How do I make my left join works?
Thank you so much!
I guess that should get you the same results without involving a sub query
select
qa.user_id as user_email,
max(i.timestamp) as session_login_time,
qa.timestamp as question_ask_time,
qa.question_id,
qa.question
from schema1.question as qa
left join schema1.user as i
on qa.user_id = i.email
and i.timestamp <= qa.timestamp
where qa.user_id <> 'tester' and qa.user_id not like '%tester.com'
group by qa.user_id, qa.timestamp, qa.question_id, qa.question

SQL Join Issues Microsoft Query

I've been trying to write some code in SQL, but it keeps coming up with a syntax error regarding the join, and I can't work out why.
SELECT `COUNTRY$`.country_name, `PARTNER$`.partner_name, count(member_id)
FROM `Member$`
Left Join `COUNTRY$`
ON `MEMBER$`.country_id=`COUNTRY$`.country_id
lEFT jOIN `PARTNER$`
on `MEMBER$`.partner_ID = `PARTNER$`.partner_ID
Group By country_name,Partner_name
Any help would be appreciated.
May have something to do with how your table names are in 'thisFormat$'. Also you did not specify which table member_id was coming and group by also doesn't specify which table country_name, partner_name was from.
Try putting aliases on the tablenames and see if that eliminates the problem
SELECT c.country_name, p.partner_name, count(m.member_id)
FROM `Member$` m
left join `COUNTRY$` c on c.country_id = m.country_id
left join `PARTNER$` p on p.partner_id = m.partner_id
GROUP BY c.country_name, p.partner_name

MySQL query issue with left joining a table with the results of two other tables

I am trying to select data from one table with the results of the data that is coming out from joining two different tables, but I am getting an error and I can not figure it out:
SELECT CONCAT(firstname,lastname) AS staffname
FROM `cms_users`
RIGHT JOIN
(SELECT DISTINCT CONCAT(a.firstname," ",a.lastname) AS clientname, b.doa
FROM `cms_clients` a
INNER JOIN `cms_question_report` b
ON a.id = b.cid
WHERE b.doa < '2015-04-15 23:00:00' AND b.doa > '2015-04-09 00:00:00') incidents
The error I am getting is:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'LIMIT 0, 30' at line 9
But I don't even have a LIMIT 0,30 in my query.
Your query is incomplete. You are missing the joining columns for your right join. See below, but you must replace PRIMARY_KEY with the primary key for your cms_users table. Also make sure that i.id is the correct joining column for the two tables.
SELECT CONCAT(firstname,lastname) AS staffname
FROM `cms_users`
RIGHT JOIN
(SELECT DISTINCT CONCAT(a.firstname," ",a.lastname) AS clientname, b.doa
FROM `cms_clients` a
INNER JOIN `cms_question_report` b
ON a.id = b.cid
WHERE b.doa < '2015-04-15 23:00:00' AND b.doa > '2015-04-09 00:00:00')
AS i ON i.id = cms_users.PRIMARY_KEY
I hope that helps!

LEFT JOIN sub-SELECT fails

I try to select the the rows with the newest timestamp in change_date from a table in a LEFT JOIN. I really don't know why this query fails:
SELECT
i.ID, i.title, i.create_date,
u1.username creator_name,
u2.username assignee
FROM item i
LEFT JOIN user u1 ON u1.login_IDFK = i.creator_IDFK
LEFT JOIN user u2 ON u2.login_IDFK = i.assigned_to_IDFK
LEFT JOIN (
SELECT MAX(change_date), item_IDFK FROM item_state GROUP BY item_IDFK
) AS ist ON ist.item_IDFK = i.ID
I get the following error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AS ist ON ist.item_IDFK = i.ID' at line 2 (Code: 1064)
Query works great without the last LEFT JOIN
(SELECT change_date, item_IDFK FROM item_state GROUP BY item_IDFK)
You are using a group by clause without an aggregate. Each item in the select list must either be represented in the group by clause, or be part of an aggregate expression
I.E.
(Select Max(Change_Date), item_IDFK from item_state group by item_IDFK)
try to save your last subquery in a view table, and after that, left join from that table, and see if the syntax error persists.

Query error when testing in Database SQL

I have this query and appearently it's faulty?
I'm trying to join fices with mems so I can have the ficeID along with all the results from mems(These queries work individually). What am I doing wrong?
SELECT *
FROM mems
WHERE deleted <> -1
ORDER BY sort_mems
LEFT JOIN SELECT ficeID
FROM fices
Result:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN SELECT ficeID FROM offices LIMIT 0, 30' at line 1
You have JOIN clause after ORDER BY. You should place it in FROM.
I suggest you define condition of a LEFT JOIN
Also, I suggest you surround you temp tables with brackets:
SELECT m.*, t1.officeID
FROM members m
LEFT JOIN offices t1
ON m.memberID = t1.memberID
WHERE m.deleted <> -1
ORDER BY m.sort_membername;
Yes, you have the LEFT JOIN in the wrong spot (it should go after your FROM clause, and you also seem to be missing a join criteria (the ON part, this tells the database how these tables are related):
SELECT *
FROM mems m
LEFT JOIN fices f
ON m.??? = f.???
WHERE deleted <> -1
ORDER BY sort_mems