SQL基于我的月份示例

时间:2016-10-11 15:04:48

标签: sql sql-server

我自己是SQL和Learning的新手。我想知道是否有人可以帮我指导写SQL。

我有以下数据:

Sample

我使用以下查询:

SELECT 
    TIMESTAMP
    DATEPART(Year, TIMESTAMP) Year, 
    DATEPART(Month, TIMESTAMP) Month, 
    COUNT(*) [Total Rows]
FROM 
    stage.ACTIVITY_ACCUMULATOR_archive
WHERE 
    TIMESTAMP BETWEEN '01-Jan-2014' AND '30-June-2014'
GROUP BY 
    DATEPART(Year, TIMESTAMP), DATEPART(Month, TIMESTAMP)
ORDER BY 
    Year, Month

我想要实现的是在特定日期之间显示年份和月份的Timestamp,并按月和年分组。

我收到错误:

  

Msg 102,Level 15,State 1,Line 1
  “年”'

附近的语法不正确

1 个答案:

答案 0 :(得分:1)

这应该可行。选择列表中有一个额外的时间戳列。

SELECT  
    DATEPART(Year, TIMESTAMP) Year, 
    DATEPART(Month, TIMESTAMP) Month, 
    COUNT(*) [Total Rows]
FROM 
    stage.ACTIVITY_ACCUMULATOR_archive
WHERE 
    TIMESTAMP BETWEEN '01-Jan-2014' AND '30-June-2014'
GROUP BY 
    DATEPART(Year, TIMESTAMP), DATEPART(Month, TIMESTAMP)
ORDER BY 
    Year, Month