Firstly - I'm trying to figure out if this is possible to do without using a cursor. Using temporary tables and such is ok, but I'd rather avoid doing any cursors.
Here's the data (and a table)
DECLARE @TempTable TABLE(
ID INT
, Rate REAL
, Points REAL
)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 1, 4.5, 0.5)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 3, 4.625, 0.125)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 5, 4.75, -0.25)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 7, 4.875, -0.375)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 10, 5, 0.302)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 11, 5.125, 0.097)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 12, 5.25, -0.241)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 14, 5.375, -0.47)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 16, 5.5, 0)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 17, 5.625, -0.125)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 18, 5.75, -0.25)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 19, 5.875, -0.375)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 20, 6, -0.5)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 21, 6.25, -0.625)
At the beginning as the rate goes up, the points go down. Until you get to row 5 (ID 10) in which case it's Points are more than the previous row's ponts. What I need to do is write a query (or queries) that find this break point (for lack of a better term). Once I have the ID of where the data goes haywire, then I'll just delete all the bad rows - any row that comes after the breakpoint.
An even better solution would delete the row at ID 10, ID 11, ID 12, keep ID 14 since -.47 is less than -.375, then delete ID's 16,17, 19, but keep 20 and 21.
I feel that this is possible without a cursor - it would take multiple queries I think, and I have a hunch it's possible the answer is just eluding me.
Your thoughts please.
Thanks.
P.S. This is in SQL server 2000
Here's the data (and a table)
DECLARE @TempTable TABLE(
ID INT
, Rate REAL
, Points REAL
)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 1, 4.5, 0.5)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 3, 4.625, 0.125)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 5, 4.75, -0.25)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 7, 4.875, -0.375)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 10, 5, 0.302)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 11, 5.125, 0.097)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 12, 5.25, -0.241)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 14, 5.375, -0.47)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 16, 5.5, 0)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 17, 5.625, -0.125)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 18, 5.75, -0.25)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 19, 5.875, -0.375)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 20, 6, -0.5)
INSERT INTO @TempTable(ID, Rate, Points) VALUES ( 21, 6.25, -0.625)
At the beginning as the rate goes up, the points go down. Until you get to row 5 (ID 10) in which case it's Points are more than the previous row's ponts. What I need to do is write a query (or queries) that find this break point (for lack of a better term). Once I have the ID of where the data goes haywire, then I'll just delete all the bad rows - any row that comes after the breakpoint.
An even better solution would delete the row at ID 10, ID 11, ID 12, keep ID 14 since -.47 is less than -.375, then delete ID's 16,17, 19, but keep 20 and 21.
I feel that this is possible without a cursor - it would take multiple queries I think, and I have a hunch it's possible the answer is just eluding me.
Your thoughts please.
Thanks.
P.S. This is in SQL server 2000