I have to improvement this query:
SELECT pt.equip, pt.date, pt.cod,
(
SELECT pt2.date
FROM "TABLE" pt2
WHERE 1=1
AND pt2.equip = pt.equip
AND CAST(pt2.date AS DATE) between '2015/12/01' AND '2015/12/02'
AND pt2.cod in (17, 84, 85)
AND pt2.date > pt.date
ORDER BY pt2.date
LIMIT 1
)
FROM "TABLE" pt
WHERE pt.cod is not null
AND pt.equip IN (2,3,8,7)
AND CAST(pt.date AS DATE) between '2015/12/01' AND '2015/12/02'
AND pt.cod in (16, 81, 82)
The subquery is the same table of the main query.
The cod column defines the initial period in the first query, and cod defines the end of the second query.
How to make a query with the columns equip, dateInitial (first query), dateEnd (second query)???