By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE Table1 (
id INT,
name VARCHAR(255),
low INT,
high INT,
high_low_adjstmnt INT,
volume INT
);
INSERT INTO Table1 VALUES
(1,'Apple',5,3,-2,1000),
(2,'Orange',6,9,3,2000),
(3,'Banana',13,17,4,3000),
(4,'Avocado',11,19,8,4000),
(5,'Berry',21,17,-4,5000),
(6,'Peach',7,9,2,6000),
(7,'Mango',9,14,5,7000),
(8,'Grape',18,11,7,8000),
(9,'Kiwi',14,13,-1,9000);
Records: 9 Duplicates: 0 Warnings: 0
WITH cte AS (
SELECT *,
LAG(high_low_adjstmnt) OVER (ORDER BY id) PreviousRow,
LEAD(high_low_adjstmnt) OVER (ORDER BY id) NextRow
FROM Table1)
SELECT *
FROM cte
WHERE (high_low_adjstmnt < 0
OR PreviousRow < 0
OR NextRow < 0);
id | name | low | high | high_low_adjstmnt | volume | PreviousRow | NextRow |
---|---|---|---|---|---|---|---|
1 | Apple | 5 | 3 | -2 | 1000 | null | 3 |
2 | Orange | 6 | 9 | 3 | 2000 | -2 | 4 |
4 | Avocado | 11 | 19 | 8 | 4000 | 4 | -4 |
5 | Berry | 21 | 17 | -4 | 5000 | 8 | 2 |
6 | Peach | 7 | 9 | 2 | 6000 | -4 | 5 |
8 | Grape | 18 | 11 | 7 | 8000 | 5 | -1 |
9 | Kiwi | 14 | 13 | -1 | 9000 | 7 | null |