Andy Green
asked on
SQL query to get data in a specific format
Hi
I have a table with bookings in, they have a date and time, but will always be am/pm bookings.
I need to display the data as per the attached grid view image (the screen shot was done with a test static dataset).
How can I best transform my data into this shape? I'm using MS SQL 2008.
Table:
CREATE TABLE [dbo].[Bookings] (
[Id] [int] NULL,
[Name] [nvarchar] (100) COLLATE Latin1_General_CI_AS NULL,
[Owner] [nvarchar] (100) COLLATE Latin1_General_CI_AS NULL,
[Location_Id] [int] NULL,
[Location] [nvarchar] (100) COLLATE Latin1_General_CI_AS NULL,
[Start] [datetime] NULL,
[End] [datetime] NULL
)
GO
Insert demo data:
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(7,N'Test meeting 1 (8-1)',N'Site Administrator',140,N'Meeti ng Room 1','2018-01-19 08:30:00.000','2018-01-19 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(8,N'Test meeting 1 (8-1)',N'Site Administrator',140,N'Meeti ng Room 1','2018-01-20 08:30:00.000','2018-01-20 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(17,N'Test meeting 1 (8-1)',N'Site Administrator',89,N'Meetin g Room 2','2018-01-19 08:30:00.000','2018-01-19 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(18,N'Test meeting 1 (8-1)',N'Site Administrator',89,N'Meetin g Room 2','2018-01-20 08:30:00.000','2018-01-20 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(27,N'Test meeting 1 (8-1)',N'Site Administrator',92,N'Meetin g Room 3','2018-01-19 08:30:00.000','2018-01-19 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(28,N'Test meeting 1 (8-1)',N'Site Administrator',92,N'Meetin g Room 3','2018-01-20 08:30:00.000','2018-01-20 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(37,N'Test meeting 1 (8-1)',N'Site Administrator',136,N'Meeti ng Room 4','2018-01-19 08:30:00.000','2018-01-19 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(38,N'Test meeting 1 (8-1)',N'Site Administrator',136,N'Meeti ng Room 4','2018-01-20 08:30:00.000','2018-01-20 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(47,N'Test meeting 1 (8-1)',N'Site Administrator',137,N'Meeti ng Room 5','2018-01-19 08:30:00.000','2018-01-19 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(48,N'Test meeting 1 (8-1)',N'Site Administrator',137,N'Meeti ng Room 5','2018-01-20 08:30:00.000','2018-01-20 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(57,N'Test meeting 1 (8-1)',N'Site Administrator',138,N'Meeti ng Room 6','2018-01-19 08:30:00.000','2018-01-19 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(58,N'Test meeting 1 (8-1)',N'Site Administrator',138,N'Meeti ng Room 6','2018-01-20 08:30:00.000','2018-01-20 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(67,N'Test meeting 1 (8-1)',N'Site Administrator',139,N'Meeti ng Room 7','2018-01-19 08:30:00.000','2018-01-19 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(68,N'Test meeting 1 (8-1)',N'Site Administrator',139,N'Meeti ng Room 7','2018-01-20 08:30:00.000','2018-01-20 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(77,N'Test meeting 1 (8-1)',N'Site Administrator',84,N'Meetin g Room 8','2018-01-19 08:30:00.000','2018-01-19 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(78,N'Test meeting 1 (8-1)',N'Site Administrator',84,N'Meetin g Room 8','2018-01-20 08:30:00.000','2018-01-20 13:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(87,N'Test meeting 2 (1-6)',N'Site Administrator',140,N'Meeti ng Room 1','2018-01-19 14:00:00.000','2018-01-19 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(88,N'Test meeting 2 (1-6)',N'Site Administrator',140,N'Meeti ng Room 1','2018-01-20 14:00:00.000','2018-01-20 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(97,N'Test meeting 2 (1-6)',N'Site Administrator',89,N'Meetin g Room 2','2018-01-19 14:00:00.000','2018-01-19 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(98,N'Test meeting 2 (1-6)',N'Site Administrator',89,N'Meetin g Room 2','2018-01-20 14:00:00.000','2018-01-20 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(107,N'Test meeting 2 (1-6)',N'Site Administrator',92,N'Meetin g Room 3','2018-01-19 14:00:00.000','2018-01-19 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(108,N'Test meeting 2 (1-6)',N'Site Administrator',92,N'Meetin g Room 3','2018-01-20 14:00:00.000','2018-01-20 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(117,N'Test meeting 2 (1-6)',N'Site Administrator',136,N'Meeti ng Room 4','2018-01-19 14:00:00.000','2018-01-19 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(118,N'Test meeting 2 (1-6)',N'Site Administrator',136,N'Meeti ng Room 4','2018-01-20 14:00:00.000','2018-01-20 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(127,N'Test meeting 2 (1-6)',N'Site Administrator',137,N'Meeti ng Room 5','2018-01-19 14:00:00.000','2018-01-19 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(128,N'Test meeting 2 (1-6)',N'Site Administrator',137,N'Meeti ng Room 5','2018-01-20 14:00:00.000','2018-01-20 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(137,N'Test meeting 2 (1-6)',N'Site Administrator',138,N'Meeti ng Room 6','2018-01-19 14:00:00.000','2018-01-19 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(138,N'Test meeting 2 (1-6)',N'Site Administrator',138,N'Meeti ng Room 6','2018-01-20 14:00:00.000','2018-01-20 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(147,N'Test meeting 2 (1-6)',N'Site Administrator',139,N'Meeti ng Room 7','2018-01-19 14:00:00.000','2018-01-19 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(148,N'Test meeting 2 (1-6)',N'Site Administrator',139,N'Meeti ng Room 7','2018-01-20 14:00:00.000','2018-01-20 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(157,N'Test meeting 2 (1-6)',N'Site Administrator',84,N'Meetin g Room 8','2018-01-19 14:00:00.000','2018-01-19 18:30:00.000')
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca tion_Id],[ Location], [Start],[E nd]) VALUES(158,N'Test meeting 2 (1-6)',N'Site Administrator',84,N'Meetin g Room 8','2018-01-20 14:00:00.000','2018-01-20 18:30:00.000')
GO
This is my demo fixed data set just so yo ucan see how I got the look I'm after:
Select 1 ID, 'Room 1' Room, '08:00-13:00 Dr Adiyodi' Monday, '08:00-13:00 Dr Adiyodi' Tuesday, '08:00-13:00 Dr Adiyodi' Wednesday, '08:00-13:00 Dr Adiyodi' Thursday, '08:00-13:00 Dr Adiyodi' Friday
Union
Select 2 ID, 'Room 1' Room, '14:00-17:00 Dr Durgam' Monday, '14:00-17:00 Dr Durgam' Tuesday, '14:00-17:00 Dr Durgam' Wednesday, '14:00-17:00 Dr Durgam' Thursday, '14:00-17:00 Dr Durgam' Friday
Union
Select 3 ID, 'Room 2' Room, '08:00-13:00 Dr Adiyodi' Monday, '08:00-13:00 Dr Adiyodi' Tuesday, '08:00-13:00 Dr Adiyodi' Wednesday, '08:00-13:00 Dr Adiyodi' Thursday, '08:00-13:00 Dr Adiyodi' Friday
Union
Select 4 ID, 'Room 2' Room, '' Monday, '14:00-17:00 Dr Durgam' Tuesday, '14:00-17:00 Dr Durgam' Wednesday, '14:00-17:00 Dr Durgam' Thursday, '14:00-17:00 Dr Durgam' Friday
Union
Select 5 ID, 'Room 3' Room, '08:00-13:00 Dr Adiyodi' Monday, '08:00-13:00 Dr Adiyodi' Tuesday, '' Wednesday, '08:00-13:00 Dr Adiyodi' Thursday, '08:00-13:00 Dr Adiyodi' Friday
Union
Select 6 ID, 'Room 3' Room, '14:00-17:00 Dr Durgam<br/>16:00-17:00 Dr Smith' Monday, '14:00-17:00 Dr Durgam' Tuesday, '14:00-17:00 Dr Durgam' Wednesday, '14:00-17:00 Dr Durgam' Thursday, '14:00-17:00 Dr Durgam' Friday
Union
Select 7 ID, 'Room 4' Room, '08:00-13:00 Dr Adiyodi' Monday, '08:00-13:00 Dr Adiyodi' Tuesday, '08:00-13:00 Dr Adiyodi' Wednesday, '08:00-13:00 Dr Adiyodi' Thursday, '08:00-13:00 Dr Adiyodi' Friday
Union
Select 8 ID, 'Room 4' Room, '14:00-17:00 Dr Durgam' Monday, '14:00-17:00 Dr Durgam' Tuesday, '14:00-17:00 Dr Durgam' Wednesday, '14:00-17:00 Dr Durgam' Thursday, '14:00-17:00 Dr Durgam' Friday
Union
Select 9 ID, 'Room 1' Room, '14:00-17:00 Dr Durgam' Monday, '14:00-17:00 Dr Durgam' Tuesday, '14:00-17:00 Dr Durgam' Wednesday, '14:00-17:00 Dr Durgam' Thursday, '14:00-17:00 Dr Durgam' Friday
Order By Room
Any help / pointers greatly appreciated.
Andy
Capture.PNG
I have a table with bookings in, they have a date and time, but will always be am/pm bookings.
I need to display the data as per the attached grid view image (the screen shot was done with a test static dataset).
How can I best transform my data into this shape? I'm using MS SQL 2008.
Table:
CREATE TABLE [dbo].[Bookings] (
[Id] [int] NULL,
[Name] [nvarchar] (100) COLLATE Latin1_General_CI_AS NULL,
[Owner] [nvarchar] (100) COLLATE Latin1_General_CI_AS NULL,
[Location_Id] [int] NULL,
[Location] [nvarchar] (100) COLLATE Latin1_General_CI_AS NULL,
[Start] [datetime] NULL,
[End] [datetime] NULL
)
GO
Insert demo data:
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
INSERT INTO [dbo].[Bookings] ([Id],[Name],[Owner],[Loca
GO
This is my demo fixed data set just so yo ucan see how I got the look I'm after:
Select 1 ID, 'Room 1' Room, '08:00-13:00 Dr Adiyodi' Monday, '08:00-13:00 Dr Adiyodi' Tuesday, '08:00-13:00 Dr Adiyodi' Wednesday, '08:00-13:00 Dr Adiyodi' Thursday, '08:00-13:00 Dr Adiyodi' Friday
Union
Select 2 ID, 'Room 1' Room, '14:00-17:00 Dr Durgam' Monday, '14:00-17:00 Dr Durgam' Tuesday, '14:00-17:00 Dr Durgam' Wednesday, '14:00-17:00 Dr Durgam' Thursday, '14:00-17:00 Dr Durgam' Friday
Union
Select 3 ID, 'Room 2' Room, '08:00-13:00 Dr Adiyodi' Monday, '08:00-13:00 Dr Adiyodi' Tuesday, '08:00-13:00 Dr Adiyodi' Wednesday, '08:00-13:00 Dr Adiyodi' Thursday, '08:00-13:00 Dr Adiyodi' Friday
Union
Select 4 ID, 'Room 2' Room, '' Monday, '14:00-17:00 Dr Durgam' Tuesday, '14:00-17:00 Dr Durgam' Wednesday, '14:00-17:00 Dr Durgam' Thursday, '14:00-17:00 Dr Durgam' Friday
Union
Select 5 ID, 'Room 3' Room, '08:00-13:00 Dr Adiyodi' Monday, '08:00-13:00 Dr Adiyodi' Tuesday, '' Wednesday, '08:00-13:00 Dr Adiyodi' Thursday, '08:00-13:00 Dr Adiyodi' Friday
Union
Select 6 ID, 'Room 3' Room, '14:00-17:00 Dr Durgam<br/>16:00-17:00 Dr Smith' Monday, '14:00-17:00 Dr Durgam' Tuesday, '14:00-17:00 Dr Durgam' Wednesday, '14:00-17:00 Dr Durgam' Thursday, '14:00-17:00 Dr Durgam' Friday
Union
Select 7 ID, 'Room 4' Room, '08:00-13:00 Dr Adiyodi' Monday, '08:00-13:00 Dr Adiyodi' Tuesday, '08:00-13:00 Dr Adiyodi' Wednesday, '08:00-13:00 Dr Adiyodi' Thursday, '08:00-13:00 Dr Adiyodi' Friday
Union
Select 8 ID, 'Room 4' Room, '14:00-17:00 Dr Durgam' Monday, '14:00-17:00 Dr Durgam' Tuesday, '14:00-17:00 Dr Durgam' Wednesday, '14:00-17:00 Dr Durgam' Thursday, '14:00-17:00 Dr Durgam' Friday
Union
Select 9 ID, 'Room 1' Room, '14:00-17:00 Dr Durgam' Monday, '14:00-17:00 Dr Durgam' Tuesday, '14:00-17:00 Dr Durgam' Wednesday, '14:00-17:00 Dr Durgam' Thursday, '14:00-17:00 Dr Durgam' Friday
Order By Room
Any help / pointers greatly appreciated.
Andy
Capture.PNG
ASKER
Thanks - Nearly.
This puts each booking on a separate row, I need all the days on the same row for the same location / time.
Andy
Capture1.PNG
This puts each booking on a separate row, I need all the days on the same row for the same location / time.
Andy
Capture1.PNG
First of all, I want to thank you for such a well constructed question
You gave us table, data and expected outcomes / results
Thank you.
Will be back later to contribute.
You gave us table, data and expected outcomes / results
Thank you.
Will be back later to contribute.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Mark for your kind words and solution.
It took me 5 mins to put the test script together and making it easier for the experts makes it easy for me. I mean explaining the problem re enforces my understanding too.
I have it working now. Just expanding to include the bookingID so I have a click to edit value.
Andy
It took me 5 mins to put the test script together and making it easier for the experts makes it easy for me. I mean explaining the problem re enforces my understanding too.
I have it working now. Just expanding to include the bookingID so I have a click to edit value.
Andy
ASKER
As per last comment. Thank you both, Mark was the solution I went with.
Andy
Andy
Andy,
An absolute pleasure. Enjoyed working with your requirements. It does make such a big difference :)
Regards,
Mark Wills
An absolute pleasure. Enjoyed working with your requirements. It does make such a big difference :)
Regards,
Mark Wills
Open in new window