pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Arup Rakshit <ar@zeit.io>
To: Jan Kohnert <nospam001-lists@jan-kohnert.de>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Extend inner join to fetch not yet connected rows also
Date: Sun, 22 Sep 2019 09:18:30 -0700
Message-ID: <f9705107-7f5c-06f6-d0f6-630554bd6891@aklaver.com> (raw)
In-Reply-To: <5C7E04B7-F5E3-4F2C-99A7-A8842153F531@zeit.io>
References: <7B8CBECF-09A5-492B-9E22-EDD7B0DBD469@zeit.io>
	<1780284.qfRisySgFs@kohni-mobil>
	<BBC4F372-71F2-46BA-BCF2-CCEB4F7F9F4B@zeit.io>
	<1695753.vyHXCZFGd1@kohni-mobil>
	<5C7E04B7-F5E3-4F2C-99A7-A8842153F531@zeit.io>

On 9/22/19 6:30 AM, Arup Rakshit wrote:
> 
>> On 22-Sep-2019, at 6:55 PM, Jan Kohnert <nospam001-lists@jan-kohnert.de> wrote:
>>
>> Hi Arup,
>>
>> Am Sonntag, 22. September 2019, 14:48:20 CEST schrieb Arup Rakshit:
>>> Hi Jan,
>>>
>>>> On 22-Sep-2019, at 5:38 PM, Jan Kohnert <nospam001-lists@jan-kohnert.de>
>>>> wrote:
>>>> maybe something like
>>>>
>>>> select
>>>>
>>>>    c.id,
>>>>    c.name,
>>>>    case when cs.user_id = 8 then true else false end as has
>>>>
>>>> from craftsmanships c
>>>> left join contractor_skills cs
>>>>
>>>>    on cs.craftsmanship_id = c.craftmanship_id;
>>>
>>> But this query fetched duplicate data:
>>
>> yeah, that's possible, since I don't exactly know your data model. If only the
>> values above are required, you could simply use distinct:
> 
> When someone adds a craftsmanship to their skill set, the contractor_skills table holds that relationship. I don’t think distinct is the correct tool, as it will eliminate the correct data. users and craftsmanship has m:n relationship via the join table contractor_skills.
> 
> SELECT
> 	craftsmanships.id,
> 	craftsmanships.name,
> 	CASE WHEN contractor_skills.user_id IS NULL THEN
> 		FALSE
> 	ELSE
> 		TRUE
> 	END AS has
> FROM
> 	"craftsmanships"
> 	LEFT JOIN "contractor_skills" ON "contractor_skills"."craftsmanship_id" = "craftsmanships"."id"
> 	LEFT JOIN "users" ON "users"."id" = "contractor_skills"."user_id"
> WHERE (contractor_skills.user_id = 8
> 	OR contractor_skills.user_id IS NULL)
> ORDER BY
> 	"craftsmanships"."id”;
> 
> Gives correct result. Not sure if still this query has bug in it.

What I see is that the rows below with 'has' = 'f' will not have a 
user_id(implied). So I am not sure how you plan to associate that data 
with a user?

> 
> id |                 name                  | has
> ----+---------------------------------------+-----
>    1 | paint                                 | t
>    2 | drywall                               | t
>    3 | bathrooms                             | f
>    4 | kitchens                              | f
>    5 | flooring                              | f
>    6 | basements                             | f
>    7 | carpentry                             | f
>    8 | decks (displayed as decks and patios) | f
>    9 | windows (windows and doors)           | f
>   10 | countertops                           | f
>   11 | landscaping                           | f
>   12 | electrical                            | f
>   13 | plumbing                              | f
>   14 | handyman                              | f
> (14 rows)
> 
> 
> 
> 
>>
>> select distinct
>>     c.id,
>>     c.name,
>>     case when cs.user_id = 8 then true else false end as has
>> from craftsmanships c
>> left join contractor_skills cs
>>     on cs.craftsmanship_id = c.id
>> order by
>>    c.id;
>>
>> -- 
>> MfG Jan
>>
>>
>>
>>
> 
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (10+ messages)  latest in thread

Message-ID: <f9705107-7f5c-06f6-d0f6-630554bd6891@aklaver.com>
Permalink:  ../f9705107-7f5c-06f6-d0f6-630554bd6891@aklaver.com/
Also on:    postgresql.org/message-id/f9705107-7f5c-06f6-d0f6-630554bd6891@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, ar@zeit.io, nospam001-lists@jan-kohnert.de
  Subject: Re: Extend inner join to fetch not yet connected rows also
  In-Reply-To: <f9705107-7f5c-06f6-d0f6-630554bd6891@aklaver.com>
  References: <7B8CBECF-09A5-492B-9E22-EDD7B0DBD469@zeit.io> <1780284.qfRisySgFs@kohni-mobil> <BBC4F372-71F2-46BA-BCF2-CCEB4F7F9F4B@zeit.io> <1695753.vyHXCZFGd1@kohni-mobil> <5C7E04B7-F5E3-4F2C-99A7-A8842153F531@zeit.io> <f9705107-7f5c-06f6-d0f6-630554bd6891@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox