早上好,我正面临一个奇怪的数据库,我正在尝试请求它。
我的要求:
use redmine_tma_ibp;
SELECT issues.id, custom_fields.name, custom_values.value FROM redmine_tma_ibp.issues
join custom_values
on issues.id = custom_values.customized_id
join custom_fields
on custom_values.custom_field_id = custom_fields.id
join enumerations
on enumerations.id=issues.priority_id
where (enumerations.type="IssuePriority" and (enumerations.position=1 or enumerations.position=2))
and (status_id=3 or status_id=5)
and (custom_fields.name = "Date de résolution" or custom_fields.name="Date d'ouverture")
and str_to_date(custom_values.value, '%d/%m/%Y %H:%i') > date('2016-01-01');
显示以下内容:
13430 Date de résolution 20/01/2016 16:52:27
14336 Date de résolution 04/01/2016 16:07:06
15705 Date d'ouverture 02/01/2016 00:28:46
15705 Date de résolution 04/01/2016 11:11:04
15774 Date d'ouverture 05/01/2016 11:15:06
15774 Date de résolution 05/01/2016 15:55:43
15790 Date d'ouverture 05/01/2016 16:24:20
15790 Date de résolution 06/01/2016 10:42:28
15917 Date d'ouverture 07/01/2016 16:04:31
15917 Date de résolution 07/06/2016 11:43:08
15955 Date d'ouverture 11/01/2016 02:21:48
15955 Date de résolution 12/01/2016 11:19:21
15998 Date de résolution 13/01/2016 10:35:29
我需要转换我的请求,以便有一个列为“Date d'ouverture”,另一列为“Datederésolution”,对应列中的相应日期并按ID分组。
我无法更改只读的数据库结构。
请帮助我....
答案 0 :(得分:0)
它是一个支点:
SELECT issues.id,
MAX(CASE WHEN custom_fields.name='Date de résolution'
THEN custom_values.value
ELSE '-'
END) AS 'Date de résolution',
MAX(CASE WHEN custom_fields.name='Date d\'ouverture'
THEN custom_values.value
ELSE '-'
END )AS 'Date d\'ouverture'
FROM [....]
GROUP BY issues.id
我使用MAX()技巧作为分组依据。
答案 1 :(得分:0)
试试这样:
SELECT issues.id,
CASE
WHEN custom_fields.name = "Date d'ouverture" THEN custom_values.value END AS Date_d_ouverture,
WHEN custom_fields.name = "Date de résolution" THEN custom_values.value END AS Date_de_résolution
FROM
(your_query) sub