freshgrill
asked on
Sql Query question - Postgres 9.3
I have a log file (see below - actual about 1 million rows), and I get the last date for each sensor. Now I want to know how many rows back from that latest date before a success = 'f'.
How many checks in a row has the sensor been okay since the latest check- by each sensor number.
How would I write that query?
Desired Query Answer:
Sensor: Number of times success ='t' before a success = 'f'
1: 2
2:1
3:3
id, sensor number, date, success
1, 1, 2013-08-15, t
2, 1, 2013-08-14, t
3, 1, 2013-08-13, f
4, 1, 2013-08-12, t
5, 2, 2013-08-15, t
6, 2, 2013-08-14, f
7, 3, 2013-08-15, t
8, 3, 2013-08-15, t
9, 3, 2013-08-15, t
10, 3, 2013-08-15, f
11, 3, 2013-08-15, t
How many checks in a row has the sensor been okay since the latest check- by each sensor number.
How would I write that query?
Desired Query Answer:
Sensor: Number of times success ='t' before a success = 'f'
1: 2
2:1
3:3
id, sensor number, date, success
1, 1, 2013-08-15, t
2, 1, 2013-08-14, t
3, 1, 2013-08-13, f
4, 1, 2013-08-12, t
5, 2, 2013-08-15, t
6, 2, 2013-08-14, f
7, 3, 2013-08-15, t
8, 3, 2013-08-15, t
9, 3, 2013-08-15, t
10, 3, 2013-08-15, f
11, 3, 2013-08-15, t
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
see: http://sqlfiddle.com/#!1/68af8/11
>> I have no Postgresql
happily sqlfiddle offers Postgres (along with MySQL, MSSQL, Oracle & SQLite)
ID: 39413516 line 16 is missing "AND": and f2.rn = 2
ID: 39414498 Postgres insists on aliases for a subquery
no points please
>> I have no Postgresql
happily sqlfiddle offers Postgres (along with MySQL, MSSQL, Oracle & SQLite)
ID: 39413516 line 16 is missing "AND": and f2.rn = 2
ID: 39414498 Postgres insists on aliases for a subquery
no points please
you would then want to count the number for 't' before the last 'f', but after any previous 'f', right?
let's see if we can get it like this (not tested, I have no Postgresql, but I see the syntax should work)
Open in new window