I have random amount of ports and each of them can repeat with random amount of pairs(all possible pairs- A, B, C, D). My task is to pull out sorted records from database and then compare all records one by one. When i pull out sorted records, it looks like this:
record 1:
port_nbr | 1
pair | pairA
length | 30.00
add_date | 2020-06-16 00:01:13.237164
record 2:
port_nbr | 1
pair | pairA
length | 65.00
add_date | 2020-06-16 00:02:13.237164
record 3:
port_nbr | 2
pair | pairc
length | 65.00
add_date | 2020-06-16 00:02:13.237164
This is just example. Port_nbr can repeat many times, with different pairs(A-D). My task is to compare first record if it has same port_nbr with second record. If its true, then it compares if first record pair is same with second record pair. If its also true, it will calculate length changed between these two records. If change is bigger than 30, it prints it out, if not , it moves on to second/third record. It will continue doing it till there is no more records left. Task is to print out all records where length change is bigger than 30m between 2 records, But it can compare only records that has same port_nbr and pair. If its not same, it will compare other records. In this 3 record examples it should print out 1/2 record, because they has same port_nbr, pair and length change is bigger than 30m. After that, it will compare record 2 with record 3. Since they has different ports, it will compare record 3 with record 4 and etc. Till there is no more records.
My code at this moment:
import java.sql.*;
import groovy.sql.Sql
class Main{
static void main(String[] args) {
def dst_db1 = Sql.newInstance('connection.........')
dst_db1.getConnection().setAutoCommit(false)
def sql = (" select d.* from (select d.*, lead((case when length <> 'N/A' then length else length_to_fault end)::float) over (partition by port_nbr, pair order by port_nbr, pair, d.add_date) as lengthh from diags d)d limit 10")
def lastRow = [id:-1, port_nbr:-1, pair:'', lengthh:-1.0]
dst_db1.eachRow( sql ) {row ->
if( row.port_nbr == lastRow.port_nbr && row.pair == lastRow.pair){
BigDecimal lengthChange =
new BigDecimal(row.lengthh ? row.lengthh : 0 ) - new BigDecimal(lastRow.lengthh ? lastRow.lengthh :0 )
if( lengthChange > 30.0){
print "Port ${row.port_nbr}, ${row.pair} length change: $lengthChange"
println "/tbetween row ID ${lastRow.id} and ${row.id}"
}
lastRow = row
}else{
println "Key Changed"
lastRow = row
}
}
}
}
Select part is correct, i tested it. All what i print out is one time - Key changed. It should print out many records, because there is many records that has length change bigger than 30... Maybe i have problem in if statement?
Aucun commentaire:
Enregistrer un commentaire