From: | "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com> |
---|---|
To: | JORGE MALDONADO <jorgemal1960(at)gmail(dot)com> |
Cc: | pgsql-novice <pgsql-novice(at)postgresql(dot)org> |
Subject: | Re: Please advice on a query |
Date: | 2018-07-11 16:16:46 |
Message-ID: | CAKFQuwb=1r9DCzr-sPxG=JBtXRUnEFz-TLvK41KEej_9RK576g@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-novice |
On Wed, Jul 11, 2018 at 9:05 AM, JORGE MALDONADO <jorgemal1960(at)gmail(dot)com>
wrote:
> I have a table more or less like this:
>
> -----------------------------------------------------
> crs_id some data req
> -----------------------------------------------------
> 25 aaaaaaaa 21
> 25 aaaaaaaa 19
> 17 ccccccccc null
> 8 dddddddd null
> 19 eeeeeeee 15
> 19 xxxxxxxxx 37
> 15 fffffffffffffffff 10
> 10 gggggggg null
>
> I need to delete rows according to the following criteria:
>
> * Row with "crs_id = 25" must be deleted (only the second row) because
> "req = 19" and there is a row with "crs_id = 19"
>
> * Row with "crs_id = 19" must be deleted (only the first row) because "req
> = 15" and there is a row with "crs_id = 15"
>
> * Row with "crs_id = 15" must be deleted because "req = 10" and there is a
> row with "crs_id = 10"
>
> I have been struggling with this issue without a successful result.
>
>
Maybe...
DELETE FROM tbl
WHERE EXISTS (SELECT 1 FROM tbl AS cmp WHERE tbl.req_id = cmp.crs_id)
That probably can be written with USING though I find that syntax a bit
hard to read personally. Might want to compare plans if performance
matters.
David J.
From | Date | Subject | |
---|---|---|---|
Next Message | Emiliano Amilcarelli | 2018-07-26 20:18:05 | test mail |
Previous Message | JORGE MALDONADO | 2018-07-11 16:05:54 | Please advice on a query |