SELECT
Sum(Earning) AS Amount,
MONTH(DateCreated) AS Month
FROM
TheTable
WHERE
DateCreated
BETWEEN
@DateFrom AND @DateTo
GROUP BY **???**
我想显示每月的收入,如果它再次出现在明年,它不应该对月份进行分组,例如
五月,六月,七月,八月,九月,十月,十一月,十二月,一月,二月,三月,四月,五月,六月等。
如果年份不同,则应该继续列表而不进行分组,例如2015年5月和2016年5月。
谢谢
答案 0 :(得分:2)
在select
和group by
:
SELECT YEAR(DateCreated) as Year, MONTH(DateCreated) AS Month,
Sum(Earning) AS Amount
FROM TheTable
WHERE DateCreated BETWEEN @DateFrom AND @DateTo
GROUP BY YEAR(DateCreated), MONTH(DateCreated)
ORDER BY YEAR(DateCreated), MONTH(DateCreated);
答案 1 :(得分:1)
您可以按月使用功能()
SELECT
Sum(Earning) AS Amount,
MONTH(DateCreated) AS Month
FROM
TheTable
WHERE
DateCreated
BETWEEN
@DateFrom AND @DateTo
GROUP BY MONTH(DateCreated)