subQuery = "select Row_Number() OVER(PARTITION BY i.id ORDER BY expire DESC) as R,t.value,i.id,t.name,t.mode,t.cost " & _
"from tbl_doorPricingLinks i inner join tbl_doorPricingLinksTables l on i.Id = l.idLink " & _
"inner join tbl_doorPricingTablesIndValue t on t.name = l.IdTable " & _
"where i.tariff = '" & tariff & "' AND l.tariff = '" & tariff & "' AND t.tariff = '" & tariff & "' AND i.mode = 0 and l.mode = " & step & " AND l.categorie = " & ItemType & " AND i.poption = '" & poName & "' and i.value = '" & poValue & "' and…