1

这可能已经在某个地方得到了回答,但我找不到。如果有人能再次帮助我处理这个 sql 语句,我将不胜感激。

这是我到目前为止的 sql 语句:

SELECT * , Round( (Rate * TIME_TO_SEC( Total ) /3600 ) , 2) AS revenue
FROM (SELECT event.eventID, event.staffID, event.role, TIMEDIFF( Time, Pause ) 
AS Total,
CASE WHEN Position = 'Teamleader'
THEN (Teamleader)
WHEN Position = 'Waiter'
THEN (Waiter)
ELSE '0'
END AS Rate
FROM event, rates, eventoverview
WHERE Storno =0
AND event.eventID= eventoverview.eventID
AND event.clientid = rates.clientid
GROUP BY event.eventID, event.clientID)q1
GROUP BY q1.staffID

我得到的表格现在给了我每个员工和活动的总费率。但我想要实现的是每个员工的这些费率的总和。所以基本上是收入的总和。

希望可以有人帮帮我。提前致谢

4

1 回答 1

0

您可以将查询包含在子查询中,并在外部查询中执行此操作,如下所示:

SELECT *,
  SUM(revenue)
FROM
(

    SELECT * , 
      Round( (Rate * TIME_TO_SEC( Total ) /3600 ) , 2) AS revenue
    FROM 
    (
       SELECT 
         event.eventID, 
         event.staffID, 
         event.role, 
         TIMEDIFF( Time, Pause ) AS Total,
         CASE WHEN Position = 'Teamleader' THEN (Teamleader)
              WHEN Position = 'Waiter'     THEN (Waiter)
              ELSE '0'
         END AS Rate
       FROM event
       INNER JOIN rates         ON event.clientid = rates.clientid
       INNER JOIN eventoverview ON event.eventID  = eventoverview.eventID
       WHERE Storno =0
       GROUP BY event.eventID, event.clientID
    )q1
    GROUP BY q1.staffID
) AS t
GROUP BY staffID;

请注意:您可能会得到不一致的数据,由于仅使用SELECT *with GROUP BY staffID,不在GROUP BY子句中的列需要用聚合函数括起来,否则 mysql 将为其获取任意值。不建议这样做,也不是这样做的标准方法。

于 2013-05-19T06:12:28.323 回答