我被迫进入非正常状态。我有一个报告,驱动程序,警报和alert_codes表。都有关系。但是当使用驱动程序ID填充的driver_id字段创建报表时,所有后续报表也应与驱动程序关联(即使这些报表的“driver_id”列不会被填充),直到创建报告的警报其警报代码为止名称字段等于“Inside fence”。理想情况下,填充每个报告的driver_id字段会很好,但这不是我所处的情况。
所以我尝试构建最有效的查询,但是我被困在查询的一部分,该查询在指定的日期和警报代码中包含“Inside fence”字符串的报告之间抓取报告。给个月。
这是我提出的,但是给出了“运算符不存在:boolean< = character varying”错误:
SELECT reports.* FROM reports
INNER JOIN alerts ON alerts.report_id = reports.id
INNER JOIN alert_codes ON alert_codes.id = alerts.code
WHERE (reports.unit_id = 3000 AND extract(MONTH FROM reports.time) = 3
AND extract(YEAR FROM reports.time) = 2013)
BETWEEN reports.time = '2013-03-20 15:21:05.379941'
AND alert_codes.name = 'Inside fence'
ORDER BY reports.time asc
有些东西告诉我,我不能在BETWEEN子句中使用两个不同的列。任何解决方案?
答案 0 :(得分:0)
您可以执行以下操作:
SELECT reports.* FROM reports
WHERE
reports.time >= '2013-03-20 15:21:05.379941'
AND reports.time <= (
SELECT min(r2.time) from reports r2
INNER JOIN alerts ON alerts.report_id = r2.id
INNER JOIN alert_codes ON alert_codes.id = alerts.code
WHERE r2.time >= '2013-03-20 15:21:05.379941'
AND alert_codes.name = 'Inside fence')
我排除了unit_id = 3000
条款。您可以将其置于有意义的位置(在外部WHERE
或内部WHERE
)。