同时计算会话数

问题描述

我正在使用SQL Server。 我正在制作BI报告,我想计算每个应用程序和国家/地区在同一时间连接的会话总数

我有一个表 DimDateTime ,其中包含日期和时间的ID。

DateTimeKey      DateTime                  Date         Month  MonthName  Year        Hour
202007010000    2020-07-01 00:00:00.000    2020-07-01    7    juillet    2020    00:00:00
202007010001    2020-07-01 00:01:00.000    2020-07-01    7    juillet    2020    00:01:00
202007010002    2020-07-01 00:02:00.000    2020-07-01    7    juillet    2020    00:02:00
202007010003    2020-07-01 00:03:00.000    2020-07-01    7    juillet    2020    00:03:00
202007010004    2020-07-01 00:04:00.000    2020-07-01    7    juillet    2020    00:04:00
202007010005    2020-07-01 00:05:00.000    2020-07-01    7    juillet    2020    00:05:00
202007010006    2020-07-01 00:06:00.000    2020-07-01    7    juillet    2020    00:06:00
.....

我有一个名为 Application 的表,另一个名为 Country 的表,其中包含ID,有关应用程序和国家/地区的信息。

我有一个表 TestSession ,其中包含以下数据:

SessionID |StartDate            | EndDate               | Application_ID | Id_Country 
--------------------------------------------------------------------------------------
1         | 01/06/2020 23:50    |   01/07/2020 06:02    |   1            |  1
2         | 01/06/2020 23:45    |   01/07/2020 00:45    |   1            |  2
3         | 01/06/2020 23:30    |   01/07/2020 01:02    |   2            |  2
4         | 01/06/2020 23:10    |   01/07/2020 00:53    |   2            |  2
5         | 01/06/2020 23:56    |   01/07/2020 10:20    |   1            |  2
6         | 01/06/2020 23:49    |   01/07/2020 02:15    |   1            |  4
7         | 01/06/2020 22:45    |   01/06/2020 23:58    |   2            |  1
8         | 01/06/2020 23:34    |   01/07/2020 00:02    |   2            |  4
9         | 01/07/2020 00:00    |   01/07/2020 03:32    |   2            |  3
10        | 01/07/2020 00:02    |   01/07/2020 02:12    |   1            |  3
....

我想获取特定时间(分钟)的连接总数。例如: application_ID 2020/07/01 00:01

Datekey    | Application_ID |   Id_Country | Total_Connections| 
----------------------------------------------------------
202007010001| 1            |   1         | 1
202007010001| 1            |   2         | 2
202007010001| 1            |   3         | 0
202007010001| 1            |   4         | 1
202007010001| 2            |   1         | 0
202007010001| 2            |   2         | 2
202007010001| 2            |   3         | 1
202007010001| 2            |   4         | 1
202007010002| 1            |   1         | 1
202007010003| 1            |   2         | 1

....

我在下面建立了查询,它可以正常工作。但是,这需要花费大量时间进行处理。 您是否有另一种解决方案,它更简单,更快捷? 预先感谢

declare @HeureCurrent datetime;
declare @id_pays int; 
declare @id_application int;
declare @max_id_pays int;
declare @max_id_application int;
set @HeureCurrent = '2020-07-01 00:00:00'
set @id_pays = 0
set @id_application = 0
set @max_id_pays = (select MAX(id) from dbo.TestSession)
set @max_id_application =(select MAX(id_int) from dbo.TestSession)


While @HeureCurrent < '2020-07-01 00:01:00'
begin 
    while @id_pays <= @max_id_pays 
    begin
        while @id_application <= @max_id_application
        begin
            set @nombre_de_connection = (  select COUNT(sessionKey) from dbo.TestSession
                                           where StartDate < @HeureCurrent and EndDate > @HeureCurrent
                                           and [id_Application] = @id_application 
                                           and id_Country = @id_pays);
                                           
            insert into dbo.TotalConnections
            SELECT [DateTimeKey],@id_application as Application,@id_pays as IsoCounty,@nombre_de_connection as Nbr_Connection
                  FROM [dbo].[DimDateTime]
                  where DateTime = @HeureCurrent;
             set @id_application = @id_application+1
          end
      set @id_application = 0
      set @id_pays = @id_pays+1
     end
 set @HeureCurrent = DATEADD(MI,1,@HeureCurrent)
 set @id_pays = 0
end

解决方法

您可以取消数据透视,以便每个日期/时间只有一行,然后进行汇总并使用累积总和:

select v.dte,t.Application_ID,t.Id_Country,sum(inc) as inc,sum(sum(inc)) over (partition by t.Application_ID,t.Id_Country order by dte) as concurrent_users
from TestSession s cross apply
     (values (s.startdate,1),(s.enddate,-1)
     ) v(dte,inc)
group by v.dte,t.Id_Country
order by v.dte,t.Id_Country;

原始数据中每个日期/时间都有一行。您的结果中包含某种数字键,没有解释。当然,可以按天或其他时间段进一步汇总。

,

您可以使用右外部联接来获得所需的结果:

SELECT B.*,COALESCE(A.TOTAL,0) AS TOTAL FROM
 (SELECT Application_ID,Id_Country,COUNT(*) AS TOTAL FROM table1 a WHERE StartDate < '2020-01-07 00:01:00'
 AND ENDDATE > '2020-01-07 00:01:00' GROUP BY Application_ID,Id_Country) A
 RIGHT OUTER JOIN
 (SELECT DISTINCT Application_ID,Id_Country FROM table1) B
 ON (A.APPLICATION_ID = B.APPLICATION_ID AND A.Id_Country = B.Id_Country);

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...