0

Ich habe die folgende Abfrage, die rekursiv Kommentare und ihre Antworten organisiert.Wie zwei Sortieroptionen in PostgreSQL RECURSIVE haben

WITH RECURSIVE comment_tree AS (
    SELECT 
     id    AS comment_id, 
     body    AS comment_body, 
     reply_to   AS comment_reply_to, 
     1     AS level, 
     "createdAt"  AS comment_date, 
     commenter_id, 
     article_id, 
     array["createdAt"] AS path_info 
    FROM "Comments" 
    WHERE "reply_to" IS NULL 
    UNION ALL 
    SELECT 
     c.id, 
     c.body, 
     c.reply_to, 
     p.level + 1, 
     "createdAt", 
     c.commenter_id, 
     c.article_id, 
     p.path_info || c."createdAt" 
    FROM "Comments" c 
    JOIN comment_tree p ON c.reply_to = comment_id 
) 
SELECT 
    comment_id, 
    path_info, 
    comment_body, 
    comment_reply_to, 
    comment_date, 
    level, 
    U.first_name, 
    U.last_name, 
    coalesce(U.username, CAST(U.id AS VARCHAR)) AS username 
FROM comment_tree 
LEFT JOIN 
    "Users" U ON commenter_id = U.id 
     WHERE article_id = '62834723-B804-4CA1-B984-D949B1A7E1E2' 
ORDER BY path_info DESC; 

Von dem, was ich sehen kann ... das funktioniert für die Sortierung und so weit außer.

Derzeit werden die Kommentare am ältesten bis neuesten sortiert. Dann werden die Antworten korrekt unterlegt, aber ich möchte, dass die übergeordnete Liste die neueste ist.

Gibt es eine Möglichkeit, die Kind-Werte DESC und Eltern ASC zu sortieren?

z.

+----+----------+----------+ 
| id | reply_to | date | 
+----+----------+----------+ 
| C1 | null  | 01052016 | < - Oldest 
| C2 | null  | 02052016 | 
| C3 | C1  | 03052016 | 
| C4 | C1  | 04052016 | 
| C5 | null  | 05052016 | 
| C6 | C4  | 06052016 | 
| C7 | C2  | 07052016 | 
| C8 | C6  | 08052016 | < - Newest 
| |   |   | 
+----+----------+----------+ 

gewünschte Ergebnis

| C5 (Newest Parent first) 
| C2 
    | C7 
| C1 
    | C3 (Oldest Child first for all tiers below parent) 
    | C4 
    | C6 
     | C8 
+0

Nur ein Hinweis: '..., um von Fall, wenn reply_to null ist dann id Ende ab , id asc; ' – Abelisto

Antwort

0

I eine künstliche Spalte sort in der Common Table Expression einführen würde.

Mit Comments wie folgt definiert:

  Table "laurenz.Comments" 
┌───────────┬───────────────────────┬───────────┐ 
│ Column │   Type   │ Modifiers │ 
├───────────┼───────────────────────┼───────────┤ 
│ id  │ character varying(10) │ not null │ 
│ reply_to │ character varying(10) │   │ 
│ createdAt │ date     │ not null │ 
└───────────┴───────────────────────┴───────────┘ 
Indexes: 
    "comment_tree_pkey" PRIMARY KEY, btree (id) 
Foreign-key constraints: 
    "comment_tree_reply_to_fkey" FOREIGN KEY (reply_to) REFERENCES "Comments"(id) 
Referenced by: 
    TABLE ""Comments"" CONSTRAINT "comment_tree_reply_to_fkey" FOREIGN KEY (reply_to) REFERENCES "Comments"(id) 

ich so etwas schreiben würde:

WITH RECURSIVE comment_tree AS (
    SELECT id, reply_to, "createdAt", 
      CAST(current_date - "createdAt" AS text) AS sort 
     FROM "Comments" 
     WHERE reply_to IS NULL 
    UNION ALL SELECT c.id, c.reply_to, c."createdAt", 
        substring(p.sort FROM '^[^-]*') || '-' || c."createdAt" 
     FROM "Comments" c 
      JOIN comment_tree p ON c.reply_to = p.id 
) 
SELECT id, reply_to, "createdAt" 
FROM comment_tree 
ORDER BY sort; 
+0

Arbeitete wie ein Charme. Ich habe CURRENT_DATE auf CURRENT_TIMESTAMP umgestellt, weil ich den Zeitstempel verwende. Ich danke dir sehr! –

+0

Bei weiterer Überprüfung funktioniert dies nicht wie erwartet, es funktioniert für die oberste Ebene und die erste Antwort, aber nachfolgende Antwort Ebenen sind nur eine Liste nach Datum sortiert ignorieren Eltern Reihenfolge. –

+0

Richtig, das habe ich verstanden, dass du es wolltest. Die Idee ist wahrscheinlich die gleiche, wenn komplizierter - markieren Sie die Zeilen in einer Weise, die Ihrer gewünschten Reihenfolge entspricht. –

Verwandte Themen