开发者

sql query - calculating commission over 12 months

开发者 https://www.devze.com 2022-12-08 14:42 出处:网络
I\'m trying to write a query to work out how much commission that my client\'s company earns in a month as below

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

精彩评论

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