【问题标题】:How can I get the count of inserts per minute in SQL如何在 SQL 中获取每分钟的插入次数
【发布时间】:2022-01-20 11:40:26
【问题描述】:

我有一张像这样的表

id name CreatedDate
1 test1 2014-06-30 09:00:00
1 test2 2014-06-30 09:01:10
1 test3 2014-06-30 09:01:23
1 test4 2014-06-30 09:01:43
1 test5 2014-06-30 09:02:02
1 test6 2014-06-30 09:02:34
1 test7 2014-06-30 09:03:22
1 test8 2014-06-30 09:03:28
1 test9 2014-06-30 09:04:14
1 test10 2014-06-30 09:04:22
1 test11 2014-06-30 09:04:28

我想获取每分钟发生的插入次数,所以输出如下所示

Inserts Per Min Start Time End Time
1 09:00:00 09:00:00
3 09:01:10 09:01:43
2 09:02:02 09:00:34
2 09:03:22 09:03:28
3 09:04:14 09:04:28

我该怎么做? 这是我每天给我插入的代码,但我不能让它每分钟工作

Select Count(CreatedDate) as InsertsPerDay, Convert(varchar, CreatedDate, 101) as CreatedDate
From MyTable
Where DATEDIFF(day, CreatedDate, GETDATE())) < 30
Group By Convert(varchar, CreatedDate, 101)
Order By InsertsPerDay DESC

【问题讨论】:

  • 那么日期部分就不重要了?
  • 不,它只是更容易阅读,所以我可以看到每分钟发生了多少次以及发生的时间

标签: sql sql-server-2008 ssms ssms-2016


【解决方案1】:

使用子查询和滞后

declare @tmp as table(id int,   name varchar(20),   CreatedDate datetime)

insert into @tmp values(
1,'test1','2014-06-30 09:00:00')
,(1,'test2','2014-06-30 09:01:10')
,(1,'test3','2014-06-30 09:01:23')
,(1,'test4','2014-06-30 09:01:43')
,(1,'test5','2014-06-30 09:02:02')
,(1,'test6','2014-06-30 09:02:34')
,(1,'test7','2014-06-30 09:03:22')
,(1,'test8','2014-06-30 09:03:28')
,(1,'test9','2014-06-30 09:04:14')
,(1,'test1','2014-06-30 09:04:22')
,(1,'test11','2014-06-30 09:04:28')

select
IsNull(sum(case when Seconds between 0 and 60 then 1 end),0) Minute_One,
IsNull(sum(case when Seconds between 61 and 60*2 then 1 end),0) Minute_Two,
IsNull(sum(case when Seconds > 60*2 then 1 end),0) Minute_Others
from 
(
select
(DATEPART(HOUR, DiffCreatedDate) * 3600) +
(DATEPART(MINUTE, DiffCreatedDate) * 60) +
(DATEPART(SECOND, DiffCreatedDate)) Seconds 
from
(
select
CreatedDate-PriorCreatedDate DiffCreatedDate
from
(
select 
CreatedDate,
lag(CreatedDate,1) over(order by CreatedDate) PriorCreatedDate
from @tmp
)x
)y
)z
--order by Seconds

【讨论】:

    【解决方案2】:

    这可能适用于 2008 年。(但无法验证)

    Select 
      Count(CreatedDate) As [Inserts Per Min]
    , Min(Cast(CreatedDate As Time(0))) As [Start Time]
    , Max(Cast(CreatedDate As Time(0))) As [End Time]
    From MyTable
    --Where CreatedDate > DateAdd(month, -1, GetDate()) 
    Group By Convert(SmallDateTime, Convert(Char(16), CreatedDate, 120))
    Order By [Inserts Per Min] Desc;
    
    Inserts Per Min Start Time End Time
    3 09:01:10 09:01:43
    3 09:04:14 09:04:28
    2 09:02:02 09:02:34
    2 09:03:22 09:03:28
    1 09:00:00 09:00:00

    dbfiddle here

    上的演示

    【讨论】:

      【解决方案3】:
      DECLARE @Mytimes TABLE
      (
          id INT IDENTITY NOT NULL PRIMARY KEY,
          name VARCHAR(10),
          CreatedDate DATETIME
      );
      
      INSERT INTO @Mytimes
      (
          [name],
          CreatedDate
      )
      VALUES
      ('test1', '2014-06-30 09:00:00'),
      ('test2', '2014-06-30 09:01:10'),
      ('test3', '2014-06-30 09:01:23'),
      ('test4', '2014-06-30 09:01:43'),
      ('test5', '2014-06-30 09:02:02'),
      ('test6', '2014-06-30 09:02:34'),
      ('test7', '2014-06-30 09:03:22'),
      ('test8', '2014-06-30 09:03:28'),
      ('test9', '2014-06-30 09:04:14'),
      ('test10', '2014-06-30 09:04:22'),
      ('test11', '2014-06-30 09:04:28');
      
      WITH TALLY
      AS (SELECT TOP (1440)
                 ROW_NUMBER() OVER (ORDER BY t1.object_id) AS N
          FROM sys.all_columns t1
              CROSS JOIN sys.all_columns t2),
           ranges
      AS (SELECT CAST(DATEADD(MINUTE, N - 1, '00:00') AS TIME(0)) AS [from],
                 CAST(DATEADD(MINUTE, N, '00:00') AS TIME(0)) AS [to]
          FROM TALLY),
           myTimes
      AS (SELECT CAST(CreatedDate AS TIME(0)) ct
          FROM @Mytimes)
      --SELECT r.[from],
      --       r.[to],
      SELECT MIN(t.ct) [from],
             MAX(t.ct) [to],
             COUNT(t.ct)
      FROM ranges r
          -- If you want all minutes regardless there is inserts
          --LEFT JOIN myTimes t
          INNER JOIN myTimes t
              ON t.ct >= r.[from]
                 AND t.ct < r.[to]
      GROUP BY r.[from],
               r.[to]
      ORDER BY r.[from];
      

      注意:在左连接的情况下,您需要编辑选择以使用合并 min(),max() 次。即:

      ...
      SELECT MIN(COALESCE(t.ct, r.[from])) [from],
             MAX(COALESCE(t.ct, r.[to])) [to],
             COUNT(t.ct)
      FROM ranges r
          LEFT JOIN myTimes t
              ON t.ct >= r.[from]
                 AND t.ct < r.[to]
      GROUP BY r.[from],
               r.[to]
      ORDER BY r.[from];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-14
        相关资源
        最近更新 更多