如何获得设备使用时间的百分比

时间:2019-03-06 13:54:09

标签: mysql sql time

我在SQL Server 2017上有表:

Usage Statistics

创建表:


INSERT INTO [dbo].[Statistics]

           ([DeviceName]
           ,[ConnectStatistics]
           ,[RunningStatistics]
           ,[DateTime]
           ,[PcName])
     VALUES
           (<DeviceName, varchar(50),>
           ,<ConnectStatistics, bit,>
           ,<RunningStatistics, bit,>
           ,<DateTime, datetime,>
           ,<PcName, varchar(50),>)
GO

INSERT INTO [dbo].[Statistics] (DeviceName,ConnectStatistics,RunningStatistics,DateTime,PcName) 
VALUES ('Regatron_1', 1 , 0 , '2019-02-27 08:00:00','PC1'),
 ('Regatron_1', 1 , 1 , '2019-02-27 08:10:00','PC1'),
 ('Regatron_1', 1 , 0 , '2019-02-27 08:50:00','PC1'),
 ('Regatron_1', 1 , 1 , '2019-02-27 09:20:00','PC1'),
 ('Regatron_1', 0 , 0 , '2019-02-27 09:40:00','PC1'),
 ('Regatron_2', 1 , 0 , '2019-02-27 08:00:00','PC2'),
 ('Regatron_2', 1 , 1 , '2019-02-27 08:10:00','PC2'),
 ('Regatron_2', 1 , 0 , '2019-02-27 08:50:00','PC2'),
 ('Regatron_2', 1 , 1 , '2019-02-27 09:20:00','PC2'),
 ('Regatron_2', 0 , 0 , '2019-02-27 09:40:00','PC2')

下表描述了ConnectStatistics和RunningStatistics的设备的测试设备使用情况统计信息,每行带有时间戳 我需要帮助来编写查询以生成结果: 每个设备使用的时间(百分比)

如果所选时间在“ 2019-02-27 08:00:00”到“ 2019-02-27 09:40:00”之间->时间范围是1.4

Regatron_1_ConnectStatistics -> from 8:00 to 09:40 = 1.4

Regatron_1_RunningStatistics -> from 08:10 to 08:50 and 09:20 to 09:40 = 1

Regatron_2_ConnectStatistics -> from 8:00 to 09:40 = 1.4

Regatron_2_RunningStatistics -> from 08:10 to 08:50 and 09:20 to 09:40 = 1 The answer:

Regatron_1_ConnectStatistics(%) = (1.4/1.4)*100

Regatron_1_RunningStatistics(%) = (1/1.4)*100

Regatron_2_ConnectStatistics(%) = (1.4/1.4)*100

Regatron_2_RunningStatistics(%) = (1/1.4)*100

有人可以帮我写一个正确的查询吗?

谢谢

0 个答案:

没有答案