按日期时间选择计数

时间:2016-07-22 09:33:02

标签: sql-server select

如何在当前月份之前选择表格的当前行数,并在过去6个月内选择组计数值?

示例数据:

Id     CreatedTime      PersonId       MonthValue
1      2015-03-01       1              100
2      2015-03-15       5              200 
3      2015-04-19       7              400 
...
...
...
980    2016-07-22       1349           100

希望将输出中的行数分组为:

Date       Total
07/2016    331
06/2016    277
05/2016    145
04/2016    100    
03/2016    69
02/2016    57

3 个答案:

答案 0 :(得分:1)

使用带日期的辅助表:

;with dateaux as (select eomonth(GETDATE()) as d, -1 as lev union all
                 select eomonth(dateadd(month,lev,getdate())), lev-1 from dateaux where lev >-6)
select format(d, 'MM/yyyy'), count(*)
 from dateaux join table1 t on dateaux.d >= t.mydate
 group by format(d, 'MM/yyyy')

dateaux输出看起来像这样:

d           lev
2016-07-31  -1
2016-06-30  -2
2016-05-31  -3
2016-04-30  -4
2016-03-31  -5
2016-02-29  -6

答案 1 :(得分:0)

您可以使用以下内容:

SELECT  LEFT(CONVERT(nvarchar(10),[DateField],112),6) as [Date],
        COUNT(*) as [Total]
FROM YourTable
WHERE DATEDIFF(month,[DateField],GETDATE()) < 6
GROUP BY LEFT(CONVERT(nvarchar(10),[DateField],112),6) 
ORDER BY LEFT(CONVERT(nvarchar(10),[DateField],112),6) DESC

在12个月的假数据上,我得到了:

Date    Total
201607  207
201606  270
201605  279
201604  270
201603  279
201602  261

如果您需要累积数据:

;WITH cte AS (
SELECT  LEFT(CONVERT(nvarchar(10),[DateField],112),6) as [Date],
        COUNT(*) as [Total]
FROM YourTable
WHERE DATEDIFF(month,[DateField],GETDATE()) < 6
GROUP BY LEFT(CONVERT(nvarchar(10),[DateField],112),6) 
)

SELECT  c1.[Date],
        SUM(c2.[Total]) as [Total]
FROM cte c1
INNER JOIN cte c2
    ON c1.[Date] >= c2.[Date]
GROUP BY c1.[Date], c1.[Total]
ORDER BY c1.[Date] DESC

输出:

Date    Total
201607  1566
201606  1359
201605  1089
201604  810
201603  540
201602  261

答案 2 :(得分:0)

我假设您显示的数据是所需的输出,并且您有一个包含日期和值的表。以下是如何实现与所需输出相似的示例:

CREATE TABLE #Sales
    (
      OrderDate DATETIME ,
      SaleValue DECIMAL(5, 2)
    )

INSERT  INTO #Sales ( OrderDate , SaleValue )
    SELECT  '20160701' , 125.00
    UNION ALL
    SELECT  '20160702' , 255.00
    UNION ALL
    SELECT  '20160621' , 75.00
    UNION ALL
    SELECT  '20160522' , 95.00
    UNION ALL
    SELECT  '20160404' , 410.00
    UNION ALL
    SELECT  '20160412' , 207.00 
    UNION ALL
    SELECT  '20160415' , 107.00 
    UNION ALL
    SELECT  '20160313' , 10.00 
    UNION ALL
    SELECT  '20160207' , 179.00 
    UNION ALL
    SELECT  '20160125' , 237.00 

SELECT  YEAR(OrderDate) OrderYear ,
        MONTH(OrderDate) OrderMonth ,
        CAST(MONTH(OrderDate) AS VARCHAR(2)) + '/' 
      + CAST(YEAR(OrderDate) AS VARCHAR(4)) OrderDate ,
        COUNT(SaleValue) Total
FROM    #Sales
WHERE   YEAR(OrderDate) = YEAR(GETDATE())
        AND MONTH(OrderDate) >= MONTH(DATEADD(mm, -5, GETDATE()))
GROUP BY YEAR(OrderDate) ,
        MONTH(OrderDate)

DROP TABLE #Sales 

产地:

OrderYear   OrderMonth  OrderDate   Total
2016        2           2/2016      1
2016        3           3/2016      1
2016        4           4/2016      3
2016        5           5/2016      1
2016        6           6/2016      1
2016        7           7/2016      2