In an Azure SQL database, I have an EAV-style table AttributeValues that stores multiple attribute values, like this:
Id | Organization_Id | Attribute_Id | Value |
---|---|---|---|
1 | 1 | 1 | Old Org 1 Description |
2 | 1 | 1 | New Org 1 Description |
3 | 2 | 1 | Old Org 2 Description |
4 | 2 | 1 | New Org 2 Description |
5 | 1 | 2 | Old Org 1 Title |
6 | 1 | 2 | New Org 1 Title |
7 | 2 | 2 | Old Org 2 Title |
8 | 2 | 2 | New Org 2 Title |
These values need to be pivoted into Organization-specific rows, like this, which include the latest attribute value of each type (i.e. the top Value, ordered by AttributeValues.Id DESC):
Organization_Id | Description | Title |
---|---|---|
1 | New Org 1 Description | New Org 1 Title |
2 | New Org 2 Description | New Org 2 Title |
The additional caveat here is that the working query needs to adhere to all the restrictions for an Indexed View, meaning no PIVOT, no CTEs or derived tables, no window functions, no subqueries, no outer joins, etc., because the Description and Title fields need to have full-text indexes applied in order to be used with CONTAINS() for search. Can this be done?