为了产生努力预测,我需要结合"预订时间" (此处:sapactivities)和"计划时间" (这里:计划活动)。输出应包含唯一日/资源/通知组合所花费的每日总时数,优先考虑"预订"超过"计划"时间。
SELECT date_format(result.CurrentDate, '%a, %d.%m.%Y'),
result.Resource, result.Notification, result.Hours, result.Effort
FROM
(SELECT sapactivities.start AS 'CurrentDate',
sapactivities.activitytext AS 'Resource',
sapactivities.notification AS 'Notification',
SUM(TIMESTAMPDIFF(MINUTE,start,end)/60) AS 'Hours',
'Booked' AS 'Effort'
FROM work.sapactivities
GROUP BY sapactivities.notification,sapactivities.activitytext,sapactivities.start
UNION ALL
SELECT plannedactivities.activitydate AS 'CurrentDate',
/*CONCAT_WS(' ',resource.firstname,resource.lastname) AS 'Resource',*/
resource.sapActivityText AS 'Resource',
notification.notification AS 'Notification',
plannedactivities.hours AS 'Hours',
'Planned' AS 'Effort'
FROM work.plannedactivities
LEFT JOIN work.notification ON plannedactivities.idNotification = notification.id
LEFT JOIN work.resource ON plannedactivities.idResource = resource.id) AS result
GROUP BY result.Resource, result.Notification, DATE (result.CurrentDate),
CASE WHEN result.Effort = 'Booked' THEN 'Booked' ELSE 'Planned' END
ORDER BY result.Resource,result.CurrentDate ASC;
但是我仍然可以通过" Booked"获得日/资源/通知的重复行。和"计划"努力。我尝试了外部GROUP BY子句的多种方法,但是没有得到我正在寻找的结果......任何想法?
答案 0 :(得分:0)
你的CASE不应该像这样在SELECT语句中吗?
SELECT
date_format(result.CurrentDate, '%a, %d.%m.%Y'),
result.Resource, result.Notification, result.Hours,
(CASE WHEN result.Effort = 'Booked' THEN 'Booked' ELSE 'Planned' END)
FROM ...
顺便说一句,你只是加入了一列,不应该在联合之前在两列上做左连接吗?
希望它有所帮助......如果我们有真正的信息来尝试它会好得多:D
答案 1 :(得分:0)
通常可以使用ROW_NUMBER仿真完成,请参阅http://www.mysqltutorial.org/mysql-row_number/。如果'Booked'<'Planned'
和Hours
永远不会超过1000000,那么这个技巧就可以了
Select CurrentDate, Resource, Notification, min(Hours) MOD 100000, min(Effort)
FROM (
SELECT sapactivities.start AS 'CurrentDate',
sapactivities.activitytext AS 'Resource',
sapactivities.notification AS 'Notification',
1000000 + SUM(TIMESTAMPDIFF(MINUTE,start,end)/60) AS 'Hours',
'Booked' AS 'Effort'
FROM work.sapactivities
GROUP BY sapactivities.notification,sapactivities.activitytext,sapactivities.start
UNION ALL
SELECT plannedactivities.activitydate AS 'CurrentDate',
/*CONCAT_WS(' ',resource.firstname,resource.lastname) AS 'Resource',*/
resource.sapActivityText AS 'Resource',
notification.notification AS 'Notification',
2000000 + plannedactivities.hours AS 'Hours',
'Planned' AS 'Effort'
FROM work.plannedactivities
LEFT JOIN work.notification ON plannedactivities.idNotification = notification.id
LEFT JOIN work.resource ON plannedactivities.idResource = resource.id
) AS result
GROUP BY result.Resource, result.Notification, DATE (result.CurrentDate)