I have this table.
| Column name | Data Type |
|---|---|
| id | uuid |
| origin | uuid |
| type | varchar(31) |
| date | timestamp |
Example records:
| id | origin | type | date |
|---|---|---|---|
| 1 | 1111 | A | 2021-11-09 10:01:31.001 |
| 2 | 1111 | A | 2021-11-08 03:02:22.020 |
| 3 | 1111 | B | 2021-10-01 11:03:13.003 |
| 4 | 2222 | A | 2021-11-07 13:04:54.040 |
| 5 | 3333 | B | 2021-11-09 20:05:45.005 |
| 6 | 3333 | B | 2021-11-08 21:06:36.060 |
| 7 | 3333 | B | 2021-11-08 00:07:27.700 |
I'd like to make a SQL query that generates a report showing the percentage of origins that has each type, or both, in the last 7 days (date column). There are several records for the same origin.
Desired example result:
| PercentageOfOriginThatHasTypeAInLast7Days | PercentageOfOriginThatHasTypeBInLast7Days | PercentageOfOriginThatHasBothTypeInLast7Days |
|---|---|---|
| 66.66 | 33.33 | 0 |
How could I do that?
I'm using a PostgreSQL 11.x