Re: SQL help?

From: Saurabh Agrawal <mail(at)saurabhagrawal(dot)net>
To: drexl(at)little-beak(dot)com
Cc: pgsql-novice <pgsql-novice(at)lists(dot)postgresql(dot)org>
Subject: Re: SQL help?
Date: 2019-05-22 08:30:55
Message-ID: CAL1UH0tRqog=3v=b9s=UB-hj06LCRk7yjqfC_iyxaRUjeESK7Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

SELECT signed_id, (SELECT name FROM petitions WHERE id=signed_id), count(
signed_petitions.id) *AS* number
FROM signed_petitions
WHERE joined
*GROUP BY* signed_id
*ORDER BY* number;

Have a pk in the signed_petitions table and count those?

On Wed, May 22, 2019 at 1:48 PM Dr. Drexl Spivey <drexl(at)little-beak(dot)com>
wrote:

> Trying to do something very easy, but can't quite figure out the logic.
> Typing out the email, and see if I can't figure it out just typing it
> out.
>
> Have two tables:
>
> petitions
> id = (int)primary key
> name = text description
>
> and the following
>
> signed_petitions
> signed_id = references petitions.id (Foreign key)
> joined = boolean
>
> my goal is to list those petitions that have the most signatures, so I
> have to get a count of joined=true, and list the names.
>
> What confuses me on a fundamental level, is the usage of the Foreign
> key, reference. But, here is my best attempt.
>
> Ok, this is a lot harder than I thought, because the boolean is not
> actually a number. So I guess I will have to use a rank?
>
> SELECT
> petitions.id
> petitions.name
> RANK() OVER (PARTITION BY signed_petitions.joined) AS total_signed
> WHERE
> signed_petitions.joined = "TRUE"
> ORDER BY total_signed DESC
> LIMIT 10
>
>
>
> I am not sure how the rank() over (partition by... works), this is my
> first attempt.
>
> Would this give me the ten highest signed petitions?
>
>
>

In response to

  • SQL help? at 2019-05-22 08:16:59 from Dr. Drexl Spivey

Browse pgsql-novice by date

  From Date Subject
Next Message David G. Johnston 2019-05-22 14:53:24 Re: SQL help?
Previous Message Dr. Drexl Spivey 2019-05-22 08:16:59 SQL help?