From: | Arup Rakshit <aruprakshit1987(at)outlook(dot)com> |
---|---|
To: | pgsql-general <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Left join help |
Date: | 2017-06-24 17:38:07 |
Message-ID: | 9DA3AF67-FE8E-438A-B8A8-D19BE4310C05@outlook.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Hello Adrian,
The way John taught me this query with CTE and then how that can be using with JOIN really helped me to understand. The query below is from John and it worked.
SELECT *
FROM missions
LEFT JOIN submissions
INNER JOIN members
ON submissions.member_id = members.id<http://members.id> AND members.id<http://members.id> = 1
ON missions.id<http://missions.id> = submissions.mission_id
WHERE missions.track_id = 7
Result is attached.
When I added CASE expression to the above query it was giving me the error as I mentioned in previous email. I managed the CASE expression query with CTE as below:
with member_submissions as (
SELECT submissions.*
FROM submissions
INNER JOIN members ON submissions.member_id = members.id
AND members.id = 1
)
SELECT *, CASE WHEN member_submissions.id IS NULL THEN 'incompleted' ELSE 'completed' END AS mission_status
FROM missions
LEFT JOIN member_submissions ON missions.id = member_submissions.mission_id
WHERE missions.track_id = 7
But still would like to know why it didn’t work with JOIN.
Thanks,
- A
On Jun 24, 2017, at 10:30 PM, Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com<mailto:adrian(dot)klaver(at)aklaver(dot)com>> wrote:
On 06/24/2017 08:01 AM, Arup Rakshit wrote:
Hi,
Thanks everyone for taking time to explain this. I tried to add a case statement and getting errors. Can you tell me how should I add a column to mark which mission is completed and which is not. My try is not working. Instead of the new CASE expression, the query works as expected.
Can you show what works?
SELECT missions.*, CASE WHEN submissions.id IS NULL THEN 'incompleted' ELSE 'completed' END AS mission_status
FROM missions
LEFT JOIN submissions
INNER JOIN members
ON submissions.member_id = members.id AND members.id = 1
ON missions.id = submissions.mission_id
WHERE missions.track_id = 7
I am not how Postgres would determine which ON refers to which JOIN here?
To get back to your original request would the below work?:
SELECT
missions.*, CASE WHEN sub.id IS NULL THEN 'incompleted' ELSE 'completed' END AS mission_status
FROM
missions
LEFT JOIN
(SELECT * FROM submissions WHERE member_id = 1) AS sub
ON
missions.mission_id = sub.mission_id
WHERE
missions.track_id = 7
====
ERROR: missing FROM-clause entry for table "missions"
LINE 1: SELECT missions.*, CASE WHEN submissions.id IS NULL THEN 'in...
On Jun 24, 2017, at 4:53 AM, John W Higgins <wishdev(at)gmail(dot)com<mailto:wishdev(at)gmail(dot)com> <mailto:wishdev(at)gmail(dot)com>> wrote:
SELECT *
FROM missions
LEFT JOIN submissions
INNER JOIN members
ON submissions.member_id =members.id <http://members.id/>ANDmembers.id <http://members.id/>= 1
ONmissions.id <http://missions.id/>= submissions.mission_id
WHERE missions.track_id = 7
--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com<mailto:adrian(dot)klaver(at)aklaver(dot)com>
Attachment | Content-Type | Size |
---|---|---|
Query Results.csv | text/csv | 6.0 KB |
From | Date | Subject | |
---|---|---|---|
Next Message | Fabiana Zioti | 2017-06-25 14:34:11 | Configure Qt Creator to work with PostgreSQL to extensions development |
Previous Message | Adrian Klaver | 2017-06-24 17:00:51 | Re: Left join help |