0

我有下表

vehicles - id, name

vehicle_settings - id, settings, vehicle_id, company_id, updated_at, updated_by

vehicle_licenses - id, license_number, vehicle_id, company_id, updated_at, updated_by

users - id, name, email

一辆车可以有多个条目,vehicle_settings并且可以有多个条目vehicle_licenses 该车辆可能没有vehicle_settings或车辆许可证。

现在,我需要最后更新车辆设置或许可证的用户的所有车辆列表。我还应该能够过滤带有车辆 ID 的列表,并能够计算所有符合过滤条件的车辆的数量。

所以,我尝试了以下查询

SELECT DISTINCT ON (v.id) v.id,
(
SELECT
    JSON_BUILD_OBJECT(
      'id', u.id, 
      'name', u.name,
      'email', u.email
    )
FROM users u WHERE u.id=updates.updated_by  AND updates.updated_at = MAX(updates.updated_at) 
) AS updated_by,
MAX(updates.updated_at) AS updated_at,
COUNT(*) over () 
FROM vehicles v 
LEFT JOIN  LATERAL(
select foo.updated_by,foo.updated_at FROM (
select vs.updated_by,vs.updated_at from vehicle_settings vs WHERE vs.vehicle_id = v.id 
 UNION         
select vl.updated_by,vl.updated_at from vehicle_licenses vl WHERE vl.vehicle_id = v.id
) AS foo order by updated_at desc  LIMIT 1    
) AS updates ON TRUE
WHERE v.id 'c4ced8df-c93f-45e8-806f-f7d5f20d9d75'
GROUP BY v.id, updates.updated_by,updates.updated_at ;

此查询运行良好,但是计数具有误导性。我得到的计数比车辆的数量多。也许它也需要LEFT LATERAL JOIN我猜的条目。

4

1 回答 1

1
SELECT v.*, u.*,
       COUNT(*) FILTER (WHERE v.id = 'c4ced8df-c93f-45e8-806f-f7d5f20d9d75') OVER ()
FROM vehicles AS v
   LEFT JOIN (SELECT DISTINCT ON (vehicle_id)
                vehicle_id, updated_by
         FROM (SELECT vehicle_id, updated_by, updated_at
               FROM vehicle_settings
               UNION ALL
               SELECT vehicle_id, updated_by, updated_at
               FROM vehicle_licenses) AS x
         ORDER BY vehicle_id, updated_at DESC) AS vu
      ON v.id = vu.vehicle_id
   LEFT JOIN users AS u
      ON vu.updated_by = u.id;
于 2020-06-19T08:41:23.537 回答