SQL Server - 按月归档的组记录

时间:2011-02-17 05:56:25

标签: sql sql-server tsql

我有一个包含三个字段的表夹。

ID | Fixture | Date
-------------------------
1  | 123456  |  20110515
2  | 123446  |  20110512
3  | 123476  |  20110411
4  | 123486  |  20110310

......等等。

我需要按日期对记录进行分组,并希望得到夹具数量。

结果将显示如下示例。如何从SQL查询中获得结果?

Archives

February 2011 (3)
January 2011 (6)
December 2010 (10)
November 2010 (7)
October 2010 (5)

我需要社区帮助来解决这个问题,请帮助我。

5 个答案:

答案 0 :(得分:4)

这样的东西
DECLARE @Table TABLE(
        ID INT,
        Fixture INT,
        Date DATETIME
)
INSERT INTO @Table SELECT 1,123456,'20110515' 
INSERT INTO @Table SELECT 2,123446,'20110512' 
INSERT INTO @Table SELECT 3,123476,'20110411' 
INSERT INTO @Table SELECT 4,123486,'20110310'

;WITH Vals AS (
        SELECT  DATENAME(month,Date) + ' ' + CAST(DATEPART(year,Date) AS VARCHAR(4)) DateValue,
                ID,
                CONVERT(VARCHAR(6), Date, 112) OrderValue 
        FROM    @Table
)
SELECT  DateValue,
        COUNT(ID) Cnt
FROM    Vals
GROUP BY    DateValue,
            OrderValue
ORDER BY    OrderValue

答案 1 :(得分:3)

我相信这段代码可以满足您的需求:

SELECT
    DATENAME(m, [Date]) + ' ' + CAST(YEAR([Date]) AS VARCHAR(4)) AS ArchiveMonth
    ,COUNT(ID) AS Items
FROM
    Fixture
GROUP BY
    DATENAME(m, [Date]) + ' ' + CAST(YEAR([Date]) AS VARCHAR(4))

(dang it ...已经被打败了)

答案 2 :(得分:3)

尝试它,它有月号,你可以在你的代码或sql中更新它

 select COUNT(id), DATEPART(year, dtCreated) as y, DATEPART(MONTH,dtCreated) as mo 
    from TaxesSteps group by DATEPART(year, dtCreated), DATEPART(MONTH,dtCreated)
    order by 2 desc, 3 desc

答案 3 :(得分:2)

Declare @table table (ID bigint identity(1,1), Fixture nvarchar(100), [Date] nvarchar(100))

INSERT INTO @table values ('123456','20110515')
INSERT INTO @table values ('123256','20110410')
INSERT INTO @table values ('123356','20110511')
INSERT INTO @table values ('122456','20110503')

--select DATEPART(month,0, (cast([date],datetime) from @table
SELECT DATENAME(month, CAST([Date] as datetime))+ ' ' + DATENAME(Year,CAST([Date] as datetime)) + ' (' + CAST(COUNT(Fixture) as varchar(100)) + ') '
from @table
group by DATENAME(month, CAST([Date] as datetime))+ ' ' + DATENAME(Year,CAST([Date] as datetime))

答案 4 :(得分:1)

我给你三个选项,显示3个输出

选项#1

select   convert(char(6), Date, 112) MonthYear, count(*) CountFixtures
from     Fixture
group by convert(char(6), Date, 112)
order by convert(char(6), Date, 112)

输出#1 - 最基本的。前端可以计算月份和年份名称:

MonthYear CountFixtures
--------- -------------
201103    1
201104    1
201105    2

<小时/>

选项#2

select   datename(month, convert(datetime,convert(char(6), Date, 112)+'01'))
         + ' '
         + left(convert(char(6), Date, 112),4) MonthYear,
         count(*) CountFixtures
from     Fixture
group by convert(char(6), Date, 112)
order by convert(char(6), Date, 112)

输出#2 - 推荐。计数和日期是单独的字段

MonthYear                           CountFixtures
----------------------------------- -------------
March 2011                          1
April 2011                          1
May 2011                            2

选项#3

select   datename(month, convert(datetime,convert(char(6), Date, 112)+'01'))
         + ' '
         + left(convert(char(6), Date, 112),4)
         + ' ('
         + convert(varchar,count(*))
         + ')' FixturesByMonth
from     Fixture
group by convert(char(6), Date, 112)
order by convert(char(6), Date, 112)

输出#3 - 完全,就像你在问题中一样,带括号。但是,我坚信格式化(括号等)是前端任务,而不是SQL Server端。

FixturesByMonth
----------------
March 2011 (1)
April 2011 (1)
May 2011 (2)