我形成了一个有点傻的问题。但是,让我在一个我正在努力的例子中表达问题。
如您所见,除最后一列外,一切都是一样的。如何将结果连接到一行,其中包含最后一列的两个值。
我的查询:
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 :(得分:1)
您可以在所有GROUP BY
列上使用SELECT
(但不能在最后一列tpw.li_id_warnungen
上使用),并将最后一列汇总到一个字段中。
PostgreSQL解决方案:
您可以使用GROUP BY
和STRING_AGG
使用以下解决方案。要使用tpw.li_id_warnungen
上的STRING_AGG
列,您必须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解决方案:
您可以使用GROUP BY
和GROUP_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