2017-06-21 117 views
1

我形成了一个有点愚蠢的问题。但让我表达一个我正在努力挣扎的例子。Postgresql合并来自JOIN的相同列

我的查询给我下面得到以下结果: enter image description here

正如你所看到的,一切都只是最后一列是相同的。如何将结果与最后一列的两个值结合成一行。

我的查询:

select tp.tp_vorname as PatientFirstName, tp.tp_nachname as PatientLastName, 
     tp.tp_geburtsdatum as patientBirthday, pt.pt_id, te.te_startzeit as StartTime, 
     te.te_endzeit As EndTime, te_datecreated as DateCreated, tpw.li_id_warnungen as Warning 
from termin_patient tp 
INNER JOIN patienten_termin pt 
    on tp.tp_id = pt.tp_id 
INNER JOIN termin te 
    on te.te_id = pt.pt_id 
LEFT JOIN terminpatient_warnungen tpw 
on tp.tp_id = tpw.tp_id 
where pt.pt_id = te.te_id and tp.tp_id=91168 and 
     te.te_startzeit >= '2017-05-24' 
    AND te.te_startzeit < '2017-06-02' 
+0

你能分享你想要得到的结果呢?这会让问题更容易理解和回答。 – Mureinik

回答

1

可以在所有SELECT列使用GROUP BY(但不是最后一个 - tpw.li_id_warnungen)和最后一列聚合成一个字段。

解决方案对PostgreSQL:

您可以使用使用GROUP BYSTRING_AGG以下解决方案。要使用STRING_AGG上的列tpw.li_id_warnungen,您必须将CAST的值设置为TEXT。另一种解决方案,而不是使用CAST可能会在列上使用直接浇铸tpw.li_id_warnungen::text

SELECT 
    tp.tp_vorname AS PatientFirstName, 
    tp.tp_nachname AS PatientLastName, 
    tp.tp_geburtsdatum AS patientBirthday, 
    pt.pt_id, 
    te.te_startzeit AS StartTime, 
    te.te_endzeit AS EndTime, 
    te_datecreated AS DateCreated, 
    STRING_AGG(CAST(tpw.li_id_warnungen AS TEXT), ',') AS Warning 
FROM 
    termin_patient tp INNER JOIN patienten_termin pt ON tp.tp_id = pt.tp_id 
    INNER JOIN termin te ON te.te_id = pt.pt_id 
    LEFT JOIN terminpatient_warnungen tpw ON tp.tp_id = tpw.tp_id 
WHERE 
    pt.pt_id = te.te_id 
    AND tp.tp_id = 91168 
    AND te.te_startzeit >= '2017-05-24' 
    AND te.te_startzeit < '2017-06-02' 
GROUP BY 
    tp.tp_vorname, 
    tp.tp_nachname, 
    tp.tp_geburtsdatum, 
    pt.pt_id, 
    te.te_endzeit, 
    te_datecreated 

通过附加警告名称(在注释中描述)查询看起来像这样(无需CAST或直接投了):

SELECT 
    tp.tp_vorname AS PatientFirstName, 
    tp.tp_nachname AS PatientLastName, 
    tp.tp_geburtsdatum AS patientBirthday, 
    pt.pt_id, 
    te.te_startzeit AS StartTime, 
    te.te_endzeit AS EndTime, 
    te_datecreated AS DateCreated, 
    STRING_AGG(lip.li_name, ',') AS Warning 
FROM 
    termin_patient tp INNER JOIN patienten_termin pt ON tp.tp_id = pt.tp_id 
    INNER JOIN termin te ON te.te_id = pt.pt_id 
    LEFT JOIN terminpatient_warnungen tpw ON tp.tp_id = tpw.tp_id 
    LEFT JOIN li_patientenwarnung lip ON tpw.li_id_warnungen = lip.li_id 
WHERE 
    pt.pt_id = te.te_id 
    AND tp.tp_id = 91168 
    AND te.te_startzeit >= '2017-05-24' 
    AND te.te_startzeit < '2017-06-02' 
GROUP BY 
    tp.tp_vorname, 
    tp.tp_nachname, 
    tp.tp_geburtsdatum, 
    pt.pt_id, 
    te.te_endzeit, 
    te_datecreated 

解决方案为MySQL:

您可以在MySQL上使用以下解决方案,使用GROUP BYGROUP_CONCAT

SELECT 
    tp.tp_vorname AS PatientFirstName, 
    tp.tp_nachname AS PatientLastName, 
    tp.tp_geburtsdatum AS patientBirthday, 
    pt.pt_id, 
    te.te_startzeit AS StartTime, 
    te.te_endzeit AS EndTime, 
    te_datecreated AS DateCreated, 
    GROUP_CONCAT(tpw.li_id_warnungen) as Warning 
FROM 
    termin_patient tp INNER JOIN patienten_termin pt ON tp.tp_id = pt.tp_id 
    INNER JOIN termin te ON te.te_id = pt.pt_id 
    LEFT JOIN terminpatient_warnungen tpw ON tp.tp_id = tpw.tp_id 
    LEFT JOIN li_patientenwarnung lip ON tpw.li_id_warnungen = lip.li_id 
WHERE 
    pt.pt_id = te.te_id 
    AND tp.tp_id=91168 
    AND te.te_startzeit >= '2017-05-24' 
    AND te.te_startzeit < '2017-06-02' 
GROUP BY 
    tp.tp_vorname, 
    tp.tp_nachname, 
    tp.tp_geburtsdatum, 
    pt.pt_id, 
    te.te_endzeit, 
    te_datecreated 

通过附加警告名称(在注释中描述)查询应该是这样的:

SELECT 
    tp.tp_vorname AS PatientFirstName, 
    tp.tp_nachname AS PatientLastName, 
    tp.tp_geburtsdatum AS patientBirthday, 
    pt.pt_id, 
    te.te_startzeit AS StartTime, 
    te.te_endzeit AS EndTime, 
    te_datecreated AS DateCreated, 
    GROUP_CONCAT(lip.li_name) as Warning 
FROM 
    termin_patient tp INNER JOIN patienten_termin pt ON tp.tp_id = pt.tp_id 
    INNER JOIN termin te ON te.te_id = pt.pt_id 
    LEFT JOIN terminpatient_warnungen tpw ON tp.tp_id = tpw.tp_id 
WHERE 
    pt.pt_id = te.te_id 
    AND tp.tp_id=91168 
    AND te.te_startzeit >= '2017-05-24' 
    AND te.te_startzeit < '2017-06-02' 
GROUP BY 
    tp.tp_vorname, 
    tp.tp_nachname, 
    tp.tp_geburtsdatum, 
    pt.pt_id, 
    te.te_endzeit, 
    te_datecreated 
+0

函数string_agg(bigint,unknown)不存在 – Jacob

+0

@Jacob你没有告诉我们它是一个整数列。 – wildplasser

+0

对不起。这是一个int列 – Jacob