计算每分钟的最后10个数据

时间:2022-11-26 02:25:50

I'm trying to write a SQL Query that has to return how many transactions were made in the last 10 minutes for each minute. Like:

我正在尝试编写一个SQL查询,该查询必须返回每分钟最后10分钟内完成的事务数。喜欢:

1 minute ago : 2 transactions

1分钟前:2笔交易

2 minutes ago : 1 transaction

2分钟前:1笔交易

...

...

9 minutes ago : 3 transactions

9分钟前:3笔交易

10 minutes ago : 4 transactions

10分钟前:4笔交易

I was trying to do this query:

我试图做这个查询:

 DECLARE @N int =  10;
 DECLARE @NOW DATETIME =  GETDATE();

 WITH numbers( num ) AS (
     SELECT 1 UNION ALL
     SELECT 1 + num FROM numbers WHERE num < @N )

 SELECT num AS minute,
 ( 
     SELECT COUNT(*) AS RESULTS
     FROM  [ApiTransactions] 
     WHERE [DateUtc] > DATEADD(year, -1, @NOW)
     GROUP BY DATEPART(minute, DATEADD(minute, -num, @NOW))

 )
 FROM numbers;

I still don't know if the logic is right. What I know is that I'm receiving the error:

我仍然不知道逻辑是否正确。我所知道的是我收到了错误:

Each GROUP BY expression must contain at least one column that is not an outer reference.

每个GROUP BY表达式必须至少包含一个不是外部引用的列。

Why I'm having this error? Is there a better way to do the query?

为什么我有这个错误?有更好的方法来进行查询吗?

3 个解决方案

#1


3  

You don't need a numbers table for this, unless you need to fill in times with no transactions. I would start with this:

除非您需要填写没有交易的时间,否则您不需要数字表。我会从这开始:

SELECT DATEADD(minute, DATEDIFF(minute, 0, DateUtc), 0) as the_minute, COUNT(*)
FROM ApiTransactions
WHERE DateUtc > DATEADD(minute, -10, DateUtc)
GROUP BY DATEADD(minute, DATEDIFF(minute, 0, DateUtc), 0)
ORDER BY the_minute;

#2


0  

You can use this one:

你可以用这个:

SELECT  DATEDIFF(minute,[DateUtc],GETDATE()) as minutes_ago,
        COUNT(*) tran_count 
FROM ApiTransactions
WHERE DATEDIFF(second,[DateUtc],GETDATE()) <= 600 -- 10 minutes ago
GROUP BY DATEDIFF(minute,[DateUtc],GETDATE())

Output:

输出:

minutes_ago tran_count
0           2
1           3
2           10
3           15
4           4
5           9
6           12
7           6
8           13
9           4
10          2

#3


0  

I managed to solve my problem in two different ways:

我设法以两种不同的方式解决了我的问题:

DECLARE @N int =  10;
DECLARE @NOW DATETIME = GETDATE();

WITH numbers( num ) AS (
SELECT 1 UNION ALL
SELECT 1 + num FROM numbers WHERE num < @N )

SELECT num-1 AS minute,
( 

SELECT COUNT(*) AS RESULTS
FROM  [ApiTransactions] 
WHERE [DateUtc] > DATEADD(minute, -num, @NOW) AND [DateUtc] < DATEADD(minute, -num+1, @NOW)

)
FROM numbers;

that has as output:

有输出:

minutes_ago    amount 
0               4             (most recents. still being updated)

1               0

2               2

3               3

4               1

5               2

6               1

7               2

8               1

9               3

and with:

与:

DECLARE @NOW DATETIME = GETDATE();
SELECT CONVERT(int, DATEDIFF(second, [DateUtc], @NOW)/60) as TimePassed, COUNT([TypeCode]) as Amount
FROM [ApiTransactions]
WHERE [DateUtc] >= DATEADD (minute, -10 , @NOW)
GROUP BY CONVERT(int, DATEDIFF(second, [DateUtc], @NOW)/60)
ORDER BY  CONVERT(int, DATEDIFF(second, [DateUtc], @NOW)/60)

that has as output:

有输出:

minutes_ago    amount 
0               4             (most recents. still being updated)

2               2

3               3

4               1

5               2

6               1

7               2

8               1

9               3

#1


3  

You don't need a numbers table for this, unless you need to fill in times with no transactions. I would start with this:

除非您需要填写没有交易的时间,否则您不需要数字表。我会从这开始:

SELECT DATEADD(minute, DATEDIFF(minute, 0, DateUtc), 0) as the_minute, COUNT(*)
FROM ApiTransactions
WHERE DateUtc > DATEADD(minute, -10, DateUtc)
GROUP BY DATEADD(minute, DATEDIFF(minute, 0, DateUtc), 0)
ORDER BY the_minute;

#2


0  

You can use this one:

你可以用这个:

SELECT  DATEDIFF(minute,[DateUtc],GETDATE()) as minutes_ago,
        COUNT(*) tran_count 
FROM ApiTransactions
WHERE DATEDIFF(second,[DateUtc],GETDATE()) <= 600 -- 10 minutes ago
GROUP BY DATEDIFF(minute,[DateUtc],GETDATE())

Output:

输出:

minutes_ago tran_count
0           2
1           3
2           10
3           15
4           4
5           9
6           12
7           6
8           13
9           4
10          2

#3


0  

I managed to solve my problem in two different ways:

我设法以两种不同的方式解决了我的问题:

DECLARE @N int =  10;
DECLARE @NOW DATETIME = GETDATE();

WITH numbers( num ) AS (
SELECT 1 UNION ALL
SELECT 1 + num FROM numbers WHERE num < @N )

SELECT num-1 AS minute,
( 

SELECT COUNT(*) AS RESULTS
FROM  [ApiTransactions] 
WHERE [DateUtc] > DATEADD(minute, -num, @NOW) AND [DateUtc] < DATEADD(minute, -num+1, @NOW)

)
FROM numbers;

that has as output:

有输出:

minutes_ago    amount 
0               4             (most recents. still being updated)

1               0

2               2

3               3

4               1

5               2

6               1

7               2

8               1

9               3

and with:

与:

DECLARE @NOW DATETIME = GETDATE();
SELECT CONVERT(int, DATEDIFF(second, [DateUtc], @NOW)/60) as TimePassed, COUNT([TypeCode]) as Amount
FROM [ApiTransactions]
WHERE [DateUtc] >= DATEADD (minute, -10 , @NOW)
GROUP BY CONVERT(int, DATEDIFF(second, [DateUtc], @NOW)/60)
ORDER BY  CONVERT(int, DATEDIFF(second, [DateUtc], @NOW)/60)

that has as output:

有输出:

minutes_ago    amount 
0               4             (most recents. still being updated)

2               2

3               3

4               1

5               2

6               1

7               2

8               1

9               3