可以在所有SELECT
列使用GROUP BY
(但不是最后一个 - tpw.li_id_warnungen
)和最后一列聚合成一个字段。
解决方案对PostgreSQL:
您可以使用使用GROUP BY
和STRING_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 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
你能分享你想要得到的结果呢?这会让问题更容易理解和回答。 – Mureinik