3
我想生成以下结果,这是我的查询
SET @count:=0;
SELECT Workout.created_by, SUM(Workout.training_load) as TL, FB_User.name
from FB_Workout
INNER join FB_User ON FB_Workout.created_by = FB_User.id
where FB_Workout.created_at between DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW()
group by created_by
order by TL desc
WITH workout_summary AS
(
SELECT
w.created_by,
SUM(w.training_load) AS TL,
u.name
FROM
FB_Workout AS w
INNER JOIN FB_User AS u ON w.created_by = u.id
WHERE w.created_at BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW()
GROUP BY created_by
)
SELECT
*,
RANK() OVER (ORDER BY TL DESC)
FROM workout_summary
WITH workout_summary AS
(
SELECT
w.created_by,
SUM(w.training_load) AS TL,
u.name
FROM
FB_Workout AS w
INNER JOIN FB_User AS u ON w.created_by = u.id
WHERE w.created_at BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW()
GROUP BY created_by
)
SELECT
*,
RANK() OVER (ORDER BY TL DESC)
FROM workout_summary