pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to select unique records in PostgreSQL
3+ messages / 3 participants
[nested] [flat]

* How to select unique records in PostgreSQL
@ 2022-11-24 12:01 Rama Krishnan <raghuldrag@gmail.com>
  2022-11-25 14:50 ` Re: How to select unique records in PostgreSQL Ron <ronljohnsonjr@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Rama Krishnan @ 2022-11-24 12:01 UTC (permalink / raw)
  To: pgsql-general

Hi All,


I want to get the unique wallet_id from this table even it was repeated on
multiple occasions I should calculate only once as well as if the wallet_id
was calculated on previous month it shouldn't be calculate on next months


create table student_id
(
student_wallet_id int,
card_id int,
created_date date
)

insert into  student_id values ('w1', 'c1', '2022-04-01')


insert into  student_id values ('w1', 'c2', '2022-04-03')


insert into  student_id values ('w1', 'c3', '2022-04-04')

insert into  student_id values ('w1', 'c4', '2022-05-01')
insert into  student_id values ('w2', 'c1', '2022-05-01')
insert into  student_id values ('w2', 'c2', '2022-05-04')
insert into  student_id values ('w3', 'c4', '2022-05-05')
insert into  student_id values ('w3', 'c6', '2022-06-03)
insert into  student_id values ('w3', 'c7', '2022-06-05')
insert into  student_id values ('w3', 'c8', '2022-06-07')
expected output:
-------------------

months      count_wallet_id
2022-04      1
2022-05      2
2022-05      0


Regards

A.Rama Krishnan

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to select unique records in PostgreSQL
  2022-11-24 12:01 How to select unique records in PostgreSQL Rama Krishnan <raghuldrag@gmail.com>
@ 2022-11-25 14:50 ` Ron <ronljohnsonjr@gmail.com>
  2022-11-25 15:05   ` Re: How to select unique records in PostgreSQL David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Ron @ 2022-11-25 14:50 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Honestly, you do it *in PostgreSQL* the same way you do it in all the other 
SQL RDBMSs.

On 11/24/22 06:01, Rama Krishnan wrote:
>
> Hi All,
>
>
> I want to get the unique wallet_id from this table even it was repeated on 
> multiple occasions I should calculate only once as well as if the 
> wallet_id was calculated on previous month it shouldn't be calculate on 
> next months
>
>
> create table student_id
> (
> student_wallet_id int,
> card_id int,
> created_date date
> )
>
> insert into student_id values ('w1', 'c1', '2022-04-01')
>
>
> insert into student_id values ('w1', 'c2', '2022-04-03')
>
>
> insert into student_id values ('w1', 'c3', '2022-04-04')
>
> insert into student_id values ('w1', 'c4', '2022-05-01')
> insert into student_id values ('w2', 'c1', '2022-05-01')
> insert into student_id values ('w2', 'c2', '2022-05-04')
> insert into student_id values ('w3', 'c4', '2022-05-05')
> insert into student_id values ('w3', 'c6', '2022-06-03)
> insert into student_id values ('w3', 'c7', '2022-06-05')
> insert into student_id values ('w3', 'c8', '2022-06-07')
> expected output:
> -------------------
>
> months count_wallet_id
> 2022-04      1
> 2022-05     2
> 2022-05     0
>
>
> Regards
>
> A.Rama Krishnan

-- 
Angular momentum makes the world go 'round.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to select unique records in PostgreSQL
  2022-11-24 12:01 How to select unique records in PostgreSQL Rama Krishnan <raghuldrag@gmail.com>
  2022-11-25 14:50 ` Re: How to select unique records in PostgreSQL Ron <ronljohnsonjr@gmail.com>
@ 2022-11-25 15:05   ` David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: David G. Johnston @ 2022-11-25 15:05 UTC (permalink / raw)
  To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

On Fri, Nov 25, 2022 at 7:50 AM Ron <ronljohnsonjr@gmail.com> wrote:

> Honestly, you do it *in PostgreSQL* the same way you do it in all the
> other SQL RDBMSs.
>

Emphasizing "in PostgreSQL" is nonsensical - it isn't like the OP specified
that they know how to do it in some other RDBMS and are trying to convert
their knowledge to PostgreSQL.



> On 11/24/22 06:01, Rama Krishnan wrote:
>
>
>
> I want to get the unique wallet_id from this table even it was repeated on
> multiple occasions I should calculate only once as well as if the wallet_id
> was calculated on previous month it shouldn't be calculate on next months
>
>
You need a subquery to compute the month in which each wallet_id should
appear (group by wallet_id with min(date) probably), then you can group on
the min(date) column and count the wallets.

David J.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2022-11-25 15:05 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-11-24 12:01 How to select unique records in PostgreSQL Rama Krishnan <raghuldrag@gmail.com>
2022-11-25 14:50 ` Ron <ronljohnsonjr@gmail.com>
2022-11-25 15:05   ` David G. Johnston <david.g.johnston@gmail.com>

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