I apologize in advance, I am certainly not a db expert, so might be asking something obvious here. I have a large postgreql table with the following (example) data :
CREATE TABLE t
(ID int, componentid varchar(30), scanresult varchar(30), eventdatetime timestamp)
;
INSERT INTO t
(ID, componentid, scanresult, eventdatetime)
VALUES
(1, 'A', '123', '2018-02-20 00:00:00'),
(2, 'A', '345','2018-02-20 00:00:01'),
(3, 'A', '?','2018-02-20 00:00:02'),
(4, 'A', '54','2018-02-21 00:00:01'),
(5, 'A', '?','2018-02-21 00:00:02'),
(6, 'B', '?','2018-02-21 00:00:03'),
(7, 'B', '457645','2018-02-22 00:00:01'),
(8, 'B', '?','2018-02-22 00:00:02'),
(9, 'B', '56465','2018-02-22 00:00:03'),
(10, 'C', '?','2018-02-23 00:00:01'),
(11, 'C', '234234','2018-02-21 00:00:03'),
(12, 'C', '33','2018-02-22 00:00:01'),
(13, 'C', '55','2018-02-22 00:00:02'),
(14, 'C', '?','2018-02-22 00:00:03'),
(15, 'C', '?','2018-02-23 00:00:01'),
(16, 'C', '?','2018-02-23 00:00:01'),
(17, 'C', '?','2018-02-23 00:00:02'),
(18, 'C', '?','2018-02-23 00:00:03');
;
What I trying to achieve is a query that counts the nummer of occurences -per component- that have a scanresult of ?. However, I must ONLY count if the latest and consecutive scanresults are ?
To make clear what I mean, for my example I am expecting:
- for component A : 1, because the last scanresult ?
- for component B : 0, last scan is not ?
- for component C : 5, because the last 5 scanresults are ? (the first ? is followed by a normal scanresult, ergo the records are not consecutive)
Can any of you help me out here?
3
Answers
@zegarek . I just found something weird. When is use these number:
The C result is off.... Do you have any idea why that is?
lag()
orlead()
windowfunctions
bad scans
db<>fiddle
It’s not possible to directly use a window function in a
filter
clause, or nest window functions directly, hence the subqueries. If you’re not a fan of indentation, they can be replaced with CTEs.Number your rows per component backwards, so as to have the last entry numbered 1, the second last 2, etc. Then iterate through a component’s rows as long as the barcode is a ‘?’ (that is a recursive CTE in SQL). Then aggregate and count.
Demo: http://sqlfiddle.com/#!17/ca1a4c/43