开发者

sql query - calculating commission over 12 months

I'm trying to write a query to work out how much commission that my client's company earns in a month as below

SELECT (rate * SUM(duration) / 180) as commission
        FROM   teacher, person, lesson 
        WHERE  person.id = teacher.person_id
     开发者_C百科   AND    lesson.teacher = person.id
        AND    MONTH(start_time) = MONTH(NOW())
        GROUP BY person.id

This is fine for working out this month, but how would I do this to give results for the past 12 months?


Use GROUP BY person.id, MONTH(start_time) and change the last AND clause in the WHERE into MONTH(NOW()) - MONTH(start_time) <= 12.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜