Hi, there is a script for a table with data below. I am trying to write a query that will take 3 parameters;
@trainername ,@classchange and @distancechange
@classchange and @distancechange can each have values of -1, 0,+1
I am trying to get a query which will find all instances of for a given trainer where the any of the horses have changes in the class and distance that match the values provided by @classchange and @distancechange.
For example, on 31/1/10 trainer Dave ran horse Clodhopper in a Class 5 Distance 6 race
on 24/1/19 trainer Dave ran Clodhopper in a Class 6 Distance 7 race.
If the input values of @classchange was +1 and @distancechange was +1 then that would be a match.
as would, any horse for trainer Dave where its next race was in a higher class and higher distance. An input of 0 in either case means that there was no change in distance or class, and -1 means that a subsequent race was in a lower class or a lower distance. I have no idea how to achieve this in sql. I just hope that I have made myself clear enough and that someone can help. Many thanks.
CREATE TABLE [dbo].[ChangesTest](
[DATE] [date] NULL,
[TRAINER] [nvarchar](255) NULL,
[HORSE] [nvarchar](255) NULL,
[CLASS] [float] NULL,
[DISTANCE] [float] NULL,
[RESULT] [float] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-01' AS Date), N'DAVE', N'AMBLE', 2, 6, 1)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-02' AS Date), N'DAVE', N'FALLSALOT', 4, 7, 3)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-03' AS Date), N'DAVE', N'PEGASUSNOT', 5, 6, 5)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-04' AS Date), N'DAVE', N'PEGASUSNOT', 4, 7, 4)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-05' AS Date), N'DAVE', N'AMBLE', 2, 5, 1)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-06' AS Date), N'DAVE', N'CLODHOPPER', 6, 7, 2)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-07' AS Date), N'DAVE', N'PEGASUSNOT', 5, 4, 1)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-08' AS Date), N'DAVE', N'FALLSALOT', 4, 3, 2)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-09' AS Date), N'DAVE', N'AMBLE', 3, 6, 7)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-10' AS Date), N'DAVE', N'CLODHOPPER', 4, 7, 3)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-11' AS Date), N'DAVE', N'AMBLE', 2, 5, 3)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-12' AS Date), N'DAVE', N'PEGASUSNOT', 5, 6, 4)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-13' AS Date), N'DAVE', N'AMBLE', 2, 5, 9)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-14' AS Date), N'DAVE', N'FALLSALOT', 6, 7, 8)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-15' AS Date), N'DAVE', N'AMBLE', 4, 7, 1)
GO
INSERT [dbo].[ChangesTest] ([DATE], [TRAINER], [HORSE], [CLASS], [DISTANCE], [RESULT]) VALUES (CAST(N'2019-01-16' AS Date), N'DAVE', N'CLODHOPPER', 4, 6, 1)
GO