I need to create a dataset based on one which is generated from a series of SQL sub-queries. The results of the sub-queries can be replicated using the following code:
CREATE TABLE Table1
(`StayID` varchar(3), `IncidentOrder` int, `TxDetails` varchar(73))
;
INSERT INTO Table1
(`StayID`, `IncidentOrder`, `TxDetails`)
VALUES
('IP1', 1, 'Ward: A9999 - 01/01/2015 - 15:23'),
('IP1', 2, 'Consultant: Joe Bloggs Specialty :GERIATRIC MEDICINE - 02/01/2015 - 08:17'),
('IP1', 3, 'Discharge - 06/02/2015 - 16:40'),
('IP2', 1, 'Consultant: Joe Bloggs - 01/01/2015 - 09:02'),
('IP2', 2, 'Consultant: Joe Bloggs Specialty :GERIATRIC MEDICINE - 02/01/2015 - 12:56'),
('IP2', 3, 'Ward: A9999 - 02/01/2015 - 19:39'),
('IP2', 4, 'Consultant: Joe Bloggs - 05/01/2015 - 08:22'),
('IP3', 1, 'Ward: A9999 - 02/01/2015 - 04:58'),
('IP3', 2, 'Consultant: Joe Bloggs Specialty :GASTROENTEROLOGY - 02/01/2015 - 07:27'),
('IP3', 3, 'Consultant: Joe Bloggs - 05/01/2015 - 09:06'),
('IP3', 4, 'Ward: A9999 Consultant: Joe Bloggs - 05/01/2015 - 16:45'),
('IP3', 5, 'Ward: A9999 Consultant: Joe Bloggs - 05/01/2015 - 17:10'),
('IP3', 6, 'Ward: A9999 - 05/01/2015 - 18:14')
;
I need to produce results as follows:
Columns: StayID, Tx1, Tx2, Tx3, Tx., Txn
with TransferDetails being populated for each matching StayID and Txn column (based on IncidentOrder).
The IncidentOrder is not fixed and could be 10, 30, or 1 for each StayID in the data set, so a static pivot is not an option.
I have tried (and failed) producing the required output using PIVOT and am hopeful someone here can help.
Thanks in advance