Führen Sie mehrere Zeilen zu einer mit mehr als einem Zeilenwert in einer Spalte zusammen

Nov 19 2020

Ich erstelle gerade eine Abfrage, um einige Daten aus meiner Datenbank abzurufen. Ich muss auf einige Informationen mit einer gemeinsamen ID in nur einer Zeile zugreifen.

Mit dieser Abfrage:

select 
        missions_answer.response_id as "response",
        crm_player."document" as "document",
        missions_question.label as "label",
        missions_answertext.body as "bill #",
        missions_answerselectmultiple.body as "product",
        missions_answerinteger.body as "answer" 
from missions_answer 
    left join missions_question on missions_answer.question_id = missions_question.id 
    left join missions_answertext on missions_answer.id = missions_answertext.answer_ptr_id 
    left join missions_answerselectmultiple on missions_answer.id = missions_answerselectmultiple.answer_ptr_id
    left join missions_answerinteger on missions_answer.id = missions_answerinteger.answer_ptr_id 
    left join missions_response on missions_answer.response_id = missions_response.id
    left join crm_player on missions_response.player_id = crm_player.id
    LEFT JOIN crm_user ON crm_player.user_id = crm_user.id
    where  missions_answer.response_id = '71788176'
    group by missions_answer.response_id, crm_player.document,missions_answertext.body,
        missions_question.label,
        missions_answerselectmultiple.body ,
        missions_answerinteger.body,
        crm_user.first_name,
        crm_user.last_name

Folgendes habe ich derzeit:

+   response    +     document    +    label    +    bill #  +    product  +  answer
-   71788176    -     79907201    -    bill #   -    26899   -             -
-   71788176    -     79907201    -    amount   -            -             -    1
-   71788176    -     79907201    -    product  -      -    {"name": "Shoes"}   -
-   71788176    -     79907201    -    price    -            -             -  25.99

Das ist was ich suche:

+   response    +     document    +    bill #  +    product  +  amount  +   price 
-   71788176    -     79907201    -    26899   -     shoes   -       1  -   25.99 

Ich habe versucht, es zu verwenden, crosstababer ich kann es immer noch nicht finden. Vielen Dank im Voraus für Hinweise oder Hilfe.

Antworten

1 S-Man Nov 19 2020 at 09:31

Von Ihrem aktuellen Status aus können Sie den Pivot einfach mithilfe der folgenden FILTERKlausel ausführen:

Demo: db <> Geige

SELECT
    response,
    document,
    MAX(bill) FILTER (WHERE label = 'bill') as bill,
    MAX(answer) FILTER (WHERE label = 'amount') as amount,
    MAX(product) FILTER (WHERE label = 'product') as product,
    MAX(answer) FILTER (WHERE label = 'price') as price
FROM t
GROUP BY response, document

Ich bin mir nicht ganz sicher, wie Ihr Originaltisch aussieht. Wenn es eher so ist:

response | document | label   | value
-------: | -------: | :------ | :----
71788176 | 79907201 | bill    | 26899
71788176 | 79907201 | amount  | 1    
71788176 | 79907201 | product | shoes
71788176 | 79907201 | price   | 25.99

Dann können Sie die Abfrage folgendermaßen ändern:

Demo: db <> Geige

SELECT
    response,
    document,
    MAX(value) FILTER (WHERE label = 'bill') as bill,
    MAX(value) FILTER (WHERE label = 'amount') as amount,
    MAX(value) FILTER (WHERE label = 'product') as product,
    MAX(value) FILTER (WHERE label = 'price') as price
FROM t
GROUP BY response, document

Bearbeiten : TO fügte den JSON-Wert zur Produktspalte hinzu:

Demo: db <> Geige

Variante 1: Sie können den Typ einfach jsonin Typ umwandeln text:

MAX(product::text) FILTER (WHERE label = 'product') as product,

Variante 2: Sie lesen den Wert aus dem "name"Attribut:

MAX(product ->> 'name') FILTER (WHERE label = 'product') as product,