Home > database >  Results based on two related criteria posgresql
Results based on two related criteria posgresql

Time:09-22

can sql search by two related criteria? If so, I would like to ask how to do it. I have two criteria, it is the personal code and the date of the personal code:

| Personal_id |    Date     |

| 12345       |  1/25/2022  |
| 1478963     |  5/13/2022  |
| 147895555   |  10/25/2022 |
| 98765432    |  1/25/2022  |

the table shows the personal codes and their signing dates. I need to find out the ID of the signed document, what it was when that particular customer signed with that personal code and that particular date.

There are about 100 options like this, so I want to know if it is possible to find that document id based on two criteria related to the personal code and date.

I don't need a complete request, I just need some function or logic to do it

CodePudding user response:

To elaborate on my comment, my first choice would be a temp table:

create temporary table dates (
  id integer,
  dt date
);

insert into dates values
(12345, '1/25/2022'),
(1478963, '5/13/2022') --  etc for the rest of your rows

Then you can:

select foo.*
from
  foo
  join dates on
    foo.personal_id = dates.id and
    foo.date = dates.dt

If you can't create a temp table, you can do the same with values, but this is ugly to me, especially as your list (100's you said) gets larger.

select foo.*
from
  foo
  join (values (12345, '1/25/2022'::date), (1478963, '5/13/2022')) dates (id, dt) on
    foo.personal_id = dates.id
  • Related