I have a table of data that I am attempting to transpose/pivot rows to columns with.
I'm au fait with PIVOT/UNPIVOT and I tried to do this however due to the aggregation used with PIVOT I was only returning 1 result which was the first Endorsement "M06" and not the other 2 Endorsements for that PolRef@
.
Here is my data example:
CREATE TABLE #temptable (
[B@] int,
[key@] varchar(24),
[Ref@] varchar(6),
[PolRef@] varchar(10),
[Sequence@] int,
[Date] datetime,
[Endnumber] varchar(4),
[Desc] varchar(76),
[Value] int,
[Specdrivers] varchar(76),
[Reg] varchar(76)
)
INSERT INTO #temptable VALUES
( 6, '484F445830314D4330310132', 'HODX01', 'HODX01MC01', 1050, N'2019-09-20T00:00:00', 'M06', 'Garaging/storage', 0, NULL, 'All' ),
( 6, '484F445830314D433031013C', 'HODX01', 'HODX01MC01', 1060, N'2019-09-20T00:00:00', '046', 'NCB deleted', 0, NULL, 'All' ),
( 6, '484F445830314D4330310146', 'HODX01', 'HODX01MC01', 1070, N'2019-09-20T00:00:00', '099', 'Limited mileage', 1500, NULL, 'All' )
DROP TABLE #temptable
Essentially I need a column for each row that says "Applicable Endorsement" for just the Endnumber
value.
Output would look like:
| B@ | PolRef@ | Applicable Endorsement | Applicable Endorsement | Applicable Endorsement |
| 6 | HODX01MC01 | M06 | 046 | 099 |
Any thoughts on how I can go about this, note there can be any number of endorsements it isn't a fixed amount.