I have the following data:
AutoIndex | Description | InvDate | UserValue | cFieldName |
---|---|---|---|---|
561498 | Sales Order | 18/10/2019 | Normal | ulIDSOrdImportance |
561498 | Sales Order | 18/10/2019 | 0 | ufIDSOrdKilometres |
561498 | Sales Order | 18/10/2019 | 16T Truck | ulIDSOrdTruckType |
561498 | Sales Order | 18/10/2019 | B | ulIDSOrdArea |
561498 | Sales Order | 18/10/2019 | lucky | ucIDSOrdSiteContactName |
561498 | Sales Order | 18/10/2019 | 8997472 | ucIDSOrdSiteTelephone |
With the following script, I'm trying to cross apply the data:
select
i.AutoIndex
, i.Description
, convert(date,InvDate) InvDate
, h.UserValue
, u.cFieldName
, ul.*
from InvNum i
join _etblUserHistLink h on i.AutoIndex = h.TableID
join _rtblUserDict u on h.UserDictID = u.idUserDict
cross apply
(
values (cFieldName,UserValue)
) ul (col,fiel)
Which gives me this results:
The respected results I require is as following:
What am I doing wrong?