From 9410388a7a326932efc1ad4b15ff80dccb45fbc0 Mon Sep 17 00:00:00 2001 From: Harald Bamberger Date: Thu, 24 Apr 2025 15:40:58 +0200 Subject: [PATCH] tune order by to sort nearest active dv first --- .../vertragsbestandteil/Dienstverhaeltnis_model.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/application/models/vertragsbestandteil/Dienstverhaeltnis_model.php b/application/models/vertragsbestandteil/Dienstverhaeltnis_model.php index 94b0c75eb..f81a2d518 100644 --- a/application/models/vertragsbestandteil/Dienstverhaeltnis_model.php +++ b/application/models/vertragsbestandteil/Dienstverhaeltnis_model.php @@ -59,7 +59,14 @@ class Dienstverhaeltnis_model extends DB_Model } $qry .=" - ORDER BY COALESCE(dv.bis, '2999-12-31'::date) DESC, dv.von DESC + ORDER BY + CASE + WHEN (COALESCE(dv.bis, '2999-12-31'::date) - NOW()::date) < 0 THEN NULL + ELSE + (COALESCE(dv.bis, '2999-12-31'::date) - NOW()::date) + END ASC NULLS LAST, + COALESCE(dv.bis, '2999-12-31'::date) DESC, + dv.von DESC "; return $this->execQuery($qry, $data);