Given the following example data:
ID | Subject | Value | Date |
---|---|---|---|
AAA | Field | Cosi | July 23 |
BBB | Amount | 99 | July 22 |
AAA | Field | Drui | July 24 |
AAA | Amount | 87 | July 23 |
I am attempting to write a TSQL Pivot or CASE Pivot about my Subject column. Ultimately, my query should be:
- Grouping by ID.
- Pivoting (or 'spreading') on Subject.
- Aggregating on Value.
- And including the respective date in its own column.
Further, when grouping by ID, I wish to only return the row with the most current date for a given subject and ID. Though I imagine this can be done in a second query.
The transformed data should look like this:
ID | FieldValue | FieldDate | AmountValue | AmountDate |
---|---|---|---|---|
AAA | Drui | July 24 | 87 | July 23 |
BBB | Null | Null | 99 | July 22 |
An answer that is only able to do one "subject" at a time would also work:
First Query
ID | FieldValue | FieldDate |
---|---|---|
AAA | Drui | July 24 |
BBB | Null | Null |
Second Query
ID | AmountValue | AmountDate |
---|---|---|
AAA | 87 | July 23 |
BBB | 99 | July 22 |
So far, I have created some really dumb Pivots and CASE pivots that ultimately did not work. So I have since started over, thinking that I am likely overcomplicating this query. Right now, I have two very simple CASE Pivots:
A CASE Pivot that does a single pivot on the "Field" subject
SELECT
[ID],
MAX(CASE WHEN [Subject] = 'Field' THEN [Value] ELSE '' END) AS FieldValue
FROM Table1
GROUP BY [ID]
And a CASE Pivot that correctly returns the latest date for a given ID
SELECT
[ID],
MAX(CASE WHEN [Subject] = 'Field' THEN [Date] ELSE '' END) AS ChangedDate
FROM Table1
GROUP BY [ID]
But I can't figure out how to - if this makes sense - combine those two steps.
I've considered that maybe I need to take the second query that correctly returns the latest date and merely JOIN the Value column, but the way I'm about to go about it seems incorrect.