SQL select,pad按时间顺序丢失了几个月

时间:2015-05-27 10:52:51

标签: sql sql-server

请注意2015-02以下组的输出中缺少2015-03SQL个月。如果一个月没有数据,我想显示月份和0。有谁知道如何去做?

SELECT convert(char(7), MeterReadDate, 121),count(*)
FROM [myTable]
where (MeterReadDate > dateadd(d,-356,getdate()))
group by  convert(char(7), MeterReadDate, 121)
order by  convert(char(7), MeterReadDate, 121)

示例数据:

YYYY-MM COUNT
2014-06 23
2014-07 42
2014-08 80
2014-09 92
2014-10 232
2014-11 88
2014-12 8
2015-01 5
2015-04 2
2015-05 1

仍然无法清除丢失的行,这里是我的地方..

DECLARE @StartDate DATETIME = dateadd(m,-12,getdate()), @EndDate DATETIME = getdate(), @DATE DATETIME

DECLARE @TEMP AS TABLE (MeterReadDate datetime)

SET @DATE = @StartDate

WHILE @DATE <= @EndDate
BEGIN
     INSERT INTO @TEMP VALUES ( @DATE)
    SET @DATE = DATEADD(MONTH,1,@DATE)
END



SELECT convert(char(7), t.MeterReadDate, 121),count(*)

  FROM @TEMP m left join
     [myTable] t
     on convert(char(7), t.MeterReadDate, 121) = convert(char(7), m.MeterReadDate, 121)

  where (t.MeterReadDate > dateadd(m,-12,getdate()))
  group by  convert(char(7), t.MeterReadDate, 121)
  order by  convert(char(7), t.MeterReadDate, 121)

3 个答案:

答案 0 :(得分:2)

您需要一份涵盖整个期间的日期/月份列表。这是使用递归CTE的一种方法:

with months as (
      select cast(getdate() - 365) as thedate
      union all
      select date_add(1, month, thedate)
      from months
      where thedate <= getdate()
     )
select convert(char(7), m.thedate, 121) as yyyy-mm, count(t.MeterReadDate)
from months m left join
     [myTable] t
     on convert(char(7), MeterReadDate, 121) = convert(char(7), m.thedate, 121)
group by convert(char(7), m.thedate, 121)
order by convert(char(7), m.thedate, 121);

答案 1 :(得分:2)

如果您不希望超出结果的minmax日期,则可以执行以下操作:

WITH    cte
          AS ( SELECT convert(char(7), MeterReadDate, 121) AS [Date], COUNT(*) AS [Count]
               FROM [myTable]
               WHERE (MeterReadDate > dateadd(d,-356,getdate()))
               GROUP by  convert(char(7), MeterReadDate, 121)
             ),
        minmax
          AS ( SELECT   CAST(MIN([Date] + '-01') AS DATE) AS mind ,
                        CAST(MAX([Date] + '-01') AS DATE) maxd
               FROM     cte
             ),
        calendar
          AS ( SELECT   mind ,
                        CONVERT(CHAR(7), mind, 121) AS cmind
               FROM     minmax
               UNION ALL
               SELECT   DATEADD(mm, 1, calendar.mind) ,
                        CONVERT(CHAR(7), DATEADD(mm, 1, calendar.mind), 121)
               FROM     calendar
                        CROSS JOIN minmax
               WHERE    calendar.mind < minmax.maxd
             )
    SELECT  c.cmind AS [Date],
            ISNULL(cte.[Count], 0) AS [Count]
    FROM    calendar c
            LEFT JOIN cte ON c.cmind = cte.[Date]
    OPTION  ( MAXRECURSION 0 )

答案 2 :(得分:0)

执行此操作的最佳方法是加入包含日历信息的表,一种方法是在不实际更改数据库模式的情况下执行此操作(并且在我看来更具动态性)将使用表变量和{ {1}}功能。

HRESULT: 0x6d
HTTP status: 500
HTTP subStatus: 1013
HTTP reason: Internal Server Error

DATEADD()DECLARE @StartDate DATETIME = '2015-01-01', @EndDate DATETIME = '2015-12-01', @DATE DATETIME DECLARE @TEMP AS TABLE ([DATE] DATETIME) SET @DATE = @StartDate WHILE @DATE <= @EndDate BEGIN INSERT INTO @TEMP VALUES (@DATE) SET @DATE = DATEADD(MONTH,1,@DATE) END SELECT * FROM @TEMP 设置为您所需的日期,然后加入您的结果集!

<强>更新

要按照上面#34;示例数据&#34;中提供的格式提取日期,这意味着您可以加入到表格中,使用:

@Start