Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hDS78-0004Zp-Jl for pgsql-general@arkaria.postgresql.org; Mon, 08 Apr 2019 11:06:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hDS75-0003uo-NA for pgsql-general@arkaria.postgresql.org; Mon, 08 Apr 2019 11:06:55 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hDS75-0003m4-7D for pgsql-general@lists.postgresql.org; Mon, 08 Apr 2019 11:06:55 +0000 Received: from mailout.toya.net.pl ([217.113.224.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hDS70-0003KP-W2 for pgsql-general@lists.postgresql.org; Mon, 08 Apr 2019 11:06:53 +0000 Received: from mail.toya.net.pl (unknown [10.9.9.167]) by mail.toya.net.pl (Postfix) with ESMTP id 415E2A319F for ; Mon, 8 Apr 2019 13:06:47 +0200 (CEST) Received: by mail.toya.net.pl (Postfix, from userid 114) id 33DE4A3288; Mon, 8 Apr 2019 13:06:47 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on newLazir-deb8.toya.net.pl X-Spam-Level: X-Spam-Status: No, score=-203.7 required=5.0 tests=ALL_TRUSTED,BAYES_00, USER_IN_WHITELIST,ZABOJCASPAMU_MSID_NODOT autolearn=no autolearn_force=no version=3.4.2 Received: from marlap (unknown [172.17.16.148]) (Authenticated sender: marius@mtvk.pl) by mail.toya.net.pl (Postfix) with ESMTPSA id ED3A2A319F; Mon, 8 Apr 2019 13:06:46 +0200 (CEST) Date: Mon, 8 Apr 2019 13:06:44 +0200 From: mariusz To: Arup Rakshit Cc: pgsql-general@lists.postgresql.org Subject: Re: SQl help to build a result with custom aliased bool column Message-ID: <20190408130644.7bffa63d@marlap> In-Reply-To: References: <6AC24472-2D13-49DD-A29E-9FB41321CCEF@zeit.io> X-Mailer: Claws Mail 3.14.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-TOYA-AV-Checked: YES List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, 8 Apr 2019 15:32:36 +0530 Arup Rakshit wrote: hi, > I am still having some bugs. I am getting duplicate in the result set. >=20 > psql (11.0, server 10.5) > Type "help" for help. >=20 > aruprakshit=3D# select * from features; > id | name=20 > ----+------ > 1 | f1 > 2 | f2 > 3 | f3 > 4 | f4 > (4 rows) >=20 > aruprakshit=3D# select * from company; > id | name=20 > ----+------ > 1 | c1 > 2 | c2 > (2 rows) >=20 > aruprakshit=3D# select * from company_features; > id | company_id | feature_id=20 > ----+------------+------------ > 1 | 1 | 1 > 2 | 1 | 2 > 3 | 2 | 3 > 4 | 1 | 3 > (4 rows) >=20 > aruprakshit=3D# SELECT > aruprakshit-# features.id, > aruprakshit-# features.name, > aruprakshit-# coalesce(company_features.company_id =3D 1, false) AS > active aruprakshit-# FROM > aruprakshit-# features > aruprakshit-# LEFT JOIN company_features ON features.id =3D > company_features.feature_id; id | name | active=20 > ----+------+-------- > 1 | f1 | t > 2 | f2 | t > 3 | f3 | f > 3 | f3 | t > 4 | f4 | f > (5 rows) >=20 > I should get in the results only 3, as total number of features are 3. not only dups, but also you read too much (not an issue with so small number of tuples, but...) what you really need is all features and subset of commpany_features with company_id =3D 1 for that specific example, not the whole company_features table something like SELECT f.id, f.name, fc.id IS NOT NULL AS active FROM features f LEFT OUTER JOIN company_features cf ON cf.company_id =3D 1 and cf.feature_id =3D f.id would be enough, or something like SELECT f.id, f.name, EXISTS (SELECT 0 FROM company_features cf WHERE cf.company_id =3D 1 AND cf.feature_id =3D f.id) AS active FROM features f or couple more ways to achieve what you want for a given company (id=3D1 here) bear in mind that with a large number of companies and proper index on company_features the optimizer could limit company_features as necessary, while your examples read everything anyway and mangle output to get proper result (with dups and bugs, but also not optimal) furthermore i see some inconsistencies with naming, like tables (relations) company, feature, companyfeature in your first mail and features, company_features downward. i wrote above examples as in your last query, but honestly i would not really agree with such naming. for me relations (yes, relation like in math background, more than table of objects) would be company, feature, company_feature, you may prefer something other but try to make it consistent regards, mariusz >=20 > Thanks, >=20 > Arup Rakshit > ar@zeit.io >=20 >=20 >=20 > > On 08-Apr-2019, at 3:28 PM, Szymon Lipi=C5=84ski > > wrote: > >=20 > > Hey, > > you could just use > >=20 > > SELECT > > features.id, > > features.name, > > company_features.company_id =3D 1 as active > >=20 > > regards, > > Szymon > >=20 > > On Mon, 8 Apr 2019 at 09:55, Arup Rakshit wrote: > > I have 2 tables Company and Feature. They are connected via a join > > table called CompanyFeature. I want to build a result set where it > > will have id, name and a custom boolean column. This boolean column > > is there to say if the feature is present for the company or not. > >=20 > > Company table: > >=20 > > | id | name |=20 > > |----|------|=20 > > | 1 | c1 |=20 > > | 2 | c2 |=20 > > | 3 | c3 |=20 > >=20 > > Feature table: > >=20 > > | id | name |=20 > > |----|------|=20 > > | 1 | f1 |=20 > > | 2 | f2 |=20 > > | 3 | f3 |=20 > >=20 > > Company Feature table: > >=20 > > | id | feature_id | company_id |=20 > > |----|------------|------------|=20 > > | 1 | 2 | 1 |=20 > > | 2 | 1 | 1 |=20 > > | 3 | 3 | 2 |=20 > >=20 > > The result should look like for company `c1`: > >=20 > > | id | name | active |=20 > > |----|------|--------|=20 > > | 1 | f1 | t |=20 > > | 2 | f2 | t |=20 > > | 3 | f3 | f |=20 > >=20 > > I tried something like: > >=20 > > SELECT > > features.id, > > features.name, > > CASE WHEN company_features.company_id =3D 1 THEN > > TRUE > > ELSE > > FALSE > > END AS active > > FROM > > features > > LEFT JOIN company_features ON company_features.feature_id =3D > > features.id > >=20 > > It works. But is there any better way to achieve this? > >=20 > >=20 > > Thanks, > >=20 > > Arup Rakshit > > ar@zeit.io > >=20 > >=20 > >=20 > >=20 > >=20 >=20 >=20 >=20