New answers tagged pivot
6
This can be done using the PIVOT function, but since it sounds like you want to change the query based on the schemaId, then you will want to use dynamic SQL.
If you had a known number of values or knew the columns for a specific schemaID, then you could hard-coded the query. A static query would be:
select loannumber,
[First Name],
[Middle Name],
...
3
You can use this pattern. For more schema fields, just expand on the 2nd to last line with all the schema field names.
select *
from (
select l.LoanNumber, s.FieldName, f.FieldValue
from Loans l
join Schemafields s on s.SchemaId = l.SchemaId
join LoanFields f on f.LoanId = l.Id and f.SchemaFieldId = s.Id) p
pivot (
...
Top 50 recent answers are included
