如何在sql中显示每月数据库?

时间:2013-03-11 21:33:53

标签: sql sql-server tsql

我有这个问题:

declare @startdate datetime='1/1/2012', @endate datetime='12/31/2012'
select * from hours1
where date_start between @startdate and @enddate

我想显示如下数据:

Jan
feb
March
...
December

请帮我解决这个问题?

2 个答案:

答案 0 :(得分:2)

我假设您要显示开始日期和结束日期之间的月份。

declare @startdate datetime='1/1/2012', 
        @endate datetime='12/31/2012'

SELECT
    DATENAME(month, date_start) 
FROM
    hours1
WHERE
    date_start between @startdate and @enddate
GROUP BY 
    DATENAME(month, date_start), 
    DATEPART(month, date_start)
ORDER BY
    DATEPART(month, date_start)

答案 1 :(得分:0)

如果您想每月汇总数据,请使用日期函数:

select count(<count_grouped_items), sum(<sum_of_grouped_items>), ...

from hours1 
where date_start between @startdate and @enddate 
group by YEAR(date_start), MONTH(date_start) 
order by YEAR(date_start) asc, MONTH(date_starts) asc