0

I'm having a table similar to this:

first last date pos
john doe 18-03-2021
harris potter 10-06-2021
john doe 10-05-2021
harris potter 14-06-2021
jessica potter 14-06-2021
kermit foster

The use case is as follow:

  • The pos column correspond to a positive covid test
  • The date column correspond to the vaccination date

To be elligible for a covid certificate, some one must either:

  • Been tested positive and have got 1 vaccine
  • Have receive 2 vaccine

I'm trying to write a query that return me: totalDose, totalRequieredDose

For exemple:

  • If he has tested positive, the totalRequiredDose is 1 and if he has got 1 vaccine, he is elligible. As such, for Harry Potter, totalDoses=1 and totalRequieredDoses=1 and is elligible
  • If he has not been tested positive, the totalRequiredDose is 2 and if he has got 2 vaccines, he is elligible. As such, for John Doe, totalDoses=2 and totalRequieredDoses=2 and is elligible
first last totalDoses totalRequieredDoses
john doe 2 2
harris potter 1 1
jessica potter 1 2
kermit foster 0 2

As Jessica Potter have a vaccine and no pos date, she must have 2 vaccines. So the value 1/2 And Kermit foster have no pos value, he is 0/2 Etc.

I'm scratching my head to write a query (or pl/sql) that could return me such table.

Could someone give me some hints ?

4

1 回答 1

1

我们可以按名字和姓氏聚合。总剂量只是非NULL疫苗接种日期的计数。对于所需的总剂量,我们可以从值 2 开始。假设该列存在非NULL日期,则该值可以被 1 抵消,pos这表明给定的人在某个时候测试为阳性。

SELECT
    first,
    last,
    COUNT(date) AS totalDoses,
    2 - (COUNT(*) FILTER (WHERE pos IS NOT NULL) > 0)::int AS totalRequieredDoses
FROM yourTable
GROUP BY
    first,
    last
ORDER BY
    COUNT(date) DESC,
    first,
    last;

下面演示链接的屏幕截图

演示

于 2021-06-12T16:01:39.467 回答