SQL计数和时间总和

时间:2015-10-17 03:47:21

标签: sql sql-server sql-server-2014

我有一组看起来像这样的数据:

 ID         Date

62  2012-06-12 05:30:57.000
202 2012-06-13 00:00:00.000
73  2012-06-17 05:25:15.000
74  2012-06-17 06:20:00.000
75  2012-06-17 10:46:03.000
76  2012-06-17 11:15:33.000
77  2012-06-17 12:17:09.000
79  2012-06-17 21:12:44.000
81  2012-06-18 12:34:45.000
82  2012-06-18 16:46:29.000
83  2012-06-19 00:21:44.000
84  2012-06-20 11:31:52.000
86  2012-06-22 23:27:38.000
87  2012-06-23 17:02:18.000
89  2012-06-25 10:05:00.000
91  2012-06-25 12:36:13.000
92  2012-06-25 15:28:36.000
93  2012-06-26 12:16:45.000
97  2012-06-27 14:03:14.000
98  2012-06-27 14:20:37.000
99  2012-06-27 16:21:21.000
114 2012-06-28 21:58:43.000
115 2012-06-29 10:46:53.000
120 2012-07-09 01:11:34.000

这种情况持续了多年。

我尝试了这个,但它不起作用:

SELECT COUNT(Q.Questionaire_ID) AS [Count], Q.Start_Date AS [Date]
FROM Questionaires as Q 
GROUP BY Q.Start_Date

我想把每个月的数量加起来 例如,如果:

Date       Count    Total
2012-06     10       10
2012-07     5        15

5 个答案:

答案 0 :(得分:2)

如果您将每个[日期]转换为日期,则会删除时间,如果您扣除当天(减1),我们将获得该月的第一天。然后分组。最后使用SUM()OVER()来形成运行总计。

也: CONVERT(varchar(7),[Date],120)生成一个YYYY-MM字符串,如果你有MS SQL 2012+,你可以使用FORMAT([Date],' yyyy-MM')代替。

SQL Fiddle

MS SQL Server 2014架构设置

CREATE TABLE Questionaires 
    ([ID] int, [Date] datetime)
;

INSERT INTO Questionaires 
    ([ID], [Date])
VALUES
    (62, '2012-06-12 05:30:57'),
    (202, '2012-06-13 00:00:00'),
    (73, '2012-06-17 05:25:15'),
    (74, '2012-06-17 06:20:00'),
    (75, '2012-06-17 10:46:03'),
    (76, '2012-06-17 11:15:33'),
    (77, '2012-06-17 12:17:09'),
    (79, '2012-06-17 21:12:44'),
    (81, '2012-06-18 12:34:45'),
    (82, '2012-06-18 16:46:29'),
    (83, '2012-06-19 00:21:44'),
    (84, '2012-06-20 11:31:52'),
    (86, '2012-06-22 23:27:38'),
    (87, '2012-06-23 17:02:18'),
    (89, '2012-06-25 10:05:00'),
    (91, '2012-06-25 12:36:13'),
    (92, '2012-06-25 15:28:36'),
    (93, '2012-06-26 12:16:45'),
    (97, '2012-06-27 14:03:14'),
    (98, '2012-06-27 14:20:37'),
    (99, '2012-06-27 16:21:21'),
    (114, '2012-06-28 21:58:43'),
    (115, '2012-06-29 10:46:53'),
    (120, '2012-07-09 01:11:34')
;

查询1

SELECT
      CONVERT(varchar(7), [Date], 120) AS yr_month
    , CountOf
    , SUM(CountOf) OVER (order by [Date]) as Total
FROM (
      SELECT
            DATEADD(DAY, -(DAY(Q.Date) - 1), CAST(Q.[Date] as Date)) AS [Date]
          , COUNT(*) AS [CountOf]
      FROM Questionaires AS Q
      GROUP BY
            DATEADD(DAY, -(DAY(Q.Date) - 1), CAST(Q.[Date] as Date))
      ) AS d

<强> Results

| yr_month | CountOf | Total |
|----------|---------|-------|
|  2012-06 |      23 |    23 |
|  2012-07 |       1 |    24 |

答案 1 :(得分:1)

这应该有用。

select str(year) + '-' + str(month) as month, total, count
from (
SELECT COUNT(Q.Questionaire_ID) AS [Count], sum(Q.[Count]) as total, MONTH(Q.Start_Date) as month, YEAR(Q.Start_Date) as year
FROM Questionaires as Q 
GROUP BY MONTH(Q.Start_Date), YEAR(Q.Start_Date)
 ) pretty

这样的事情?

这里有效:http://sqlfiddle.com/#!6/8d955/4

答案 2 :(得分:1)

通过“累积总和”,我假设你的意思是“计数”。您还需要提取年份和月份以及按这些列分组:

SELECT YEAR(Q.Start_Date) as yyyy, MONTH(Q.Start_Date) as mm,
       COUNT(Q.Questionaire_ID) AS [Count]
FROM Questionaires as Q 
GROUP BY YEAR(Q.Start_Date), MONTH(Q.Start_Date)
ORDER BY yyyy, mm;

编辑:

如果您想要累积计数,那么这也很容易完成:

SELECT YEAR(Q.Start_Date) as yyyy, MONTH(Q.Start_Date) as mm,
       COUNT(Q.Questionaire_ID) AS [Count],
       SUM(COUNT(Q.Questionaire_ID)) OVER (ORDER BY YEAR(Q.Start_Date), MONTH(Q.Start_Date)) as CumeCount
FROM Questionaires as Q 
GROUP BY YEAR(Q.Start_Date), MONTH(Q.Start_Date)
ORDER BY yyyy, mm;

答案 3 :(得分:0)

尝试仅将日期时间字段转换为日期:

SELECT COUNT(Q.Questionaire_ID) AS [Count], CONVERT(DATETIME,CAST(Q.Start_Date AS VARCHAR(12)),103) AS [Date]
FROM Questionaires as Q 
GROUP BY CONVERT(DATETIME,CAST(Q.Start_Date AS VARCHAR(12)),103)

这里我们将datetime字段转换为VARCHAR,但只取前12个字符,这不包括时间。

答案 4 :(得分:0)

    i think this example is useful for you 

CREATE TABLE RunTotalTestData (
   id    int not null identity(1,1) primary key,
   value int not null
);

INSERT INTO RunTotalTestData (value) VALUES (1);
INSERT INTO RunTotalTestData (value) VALUES (2);
INSERT INTO RunTotalTestData (value) VALUES (4);
INSERT INTO RunTotalTestData (value) VALUES (7);
INSERT INTO RunTotalTestData (value) VALUES (9);
INSERT INTO RunTotalTestData (value) VALUES (12);
INSERT INTO RunTotalTestData (value) VALUES (13);
INSERT INTO RunTotalTestData (value) VALUES (16);
INSERT INTO RunTotalTestData (value) VALUES (22);
INSERT INTO RunTotalTestData (value) VALUES (42);
INSERT INTO RunTotalTestData (value) VALUES (57);
INSERT INTO RunTotalTestData (value) VALUES (58);
INSERT INTO RunTotalTestData (value) VALUES (59);
INSERT INTO RunTotalTestData (value) VALUES (60);


SELECT a.id, a.value, (SELECT SUM(b.value)
                       FROM RunTotalTestData b
                       WHERE b.id <= a.id)
FROM   RunTotalTestData a
ORDER BY a.id;


id   value   running total
--   -----   -------------
1    1       1
2    2       3
3    4       7
4    7       14
5    9       23
6    12      35
7    13      48
8    16      64
9    22      86
10   42      128
11   57      185
12   58      243
13   59      302
14   60      362