; with result
as
(
SELECT T.AS_Transaction_Key, MAN.AS_Metadata_Attribute_Name, TV.AS_Trans_Values_Value
FROM AS_Metadata_Attributes MAN
INNER JOIN AS_Transaction_values TV ON TV.AS_Trans_Values_AS_MetadataAttributes_Key = MAN.AS_Metadata_Attributes_Key
INNER JOIN AS_TRANSACTION T ON T.AS_Transaction_Key = TV.AS_Trans_Values_AS_Transaction_Key
)
select *
from result r
pivot
(
max(AS_Trans_Values_Value)
for AS_Metadata_Attribute_Name in ([Transaction], [Submitted], [By], [RefNo], [TotalHours])
) p
KH
Time is always against us