Skip to content
Advertisement

SQL to Get Latest Field Value

I’m trying to write an SQL query (SQL Server) that returns the latest value of a field from a history table.

The table structure is basically as below:

ISSUE TABLE:

CHANGEGROUP TABLE:

CHANGEITEM TABLE:

EXPECTED RESULT:

So each change to an issue item creates 1 change group record with the date the change was made, which can then contain 1 or more change item records.

Each change item shows the field name that was changed and the new value.

I then need to link those tables together to get each issue, the latest value of the field name called ‘ONE’, and ideally the date of the latest change.

These tables are from Jira, for those familiar with that table structure.

I’ve been trying to get this to work for a while now, so far I’ve got this query:

This returns the first 2 columns I’m looking for but I’m struggling to work out how to return the final column as when I include that in the first line I get an error saying “Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.”

I’ve done a search on here and can’t find anything that exactly matches my requirements.

Advertisement

Answer

Use window functions:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement