We are using Oracle 19c. I'm trying to write a query that will convert values into a columns. Here's sample data:
TestID | UserID | TestType | Result | TestDate |
---|---|---|---|---|
0001 | 0001 | TestA01 | + | 03-AUG-17 |
0001 | 0001 | TestA01 | + | 11-MAR-23 |
0002 | 0001 | TestA02 | - | 11-MAR-23 |
0003 | 0001 | TestB08 | - | 11-MAR-23 |
0004 | 0001 | TestB17 | + | 11-MAR-23 |
0005 | 0002 | TestA01 | + | 11-MAR-23 |
0006 | 0002 | TestB08 | + | 11-MAR-23 |
0007 | 0003 | TestA02 | - | 11-MAR-23 |
Here's how I'd like to display the data:
AnimalID | A01 | A02 | B08 | B17 |
---|---|---|---|---|
0001 | + | - | - | + |
0002 | + | U | U | + |
0003 | U | - | U | U |
My constraints - only take the latest test date (entirely possible that there are multiple results for each User); list all 4 test types and if no value, list 'U' or blank.
I know how to do ListAgg over values, but I don't know how to force display of all 4 values even when some don't exist. I've done it using four subqueries (one for each A01, A02 B08, B17) but the query performance is then far too slow. I welcome your suggestions.