我正在使用Microsoft SQL Server,我有3列:
Server State Date
SRV1 True 2019-01-01
SRV2 False 2019-01-01
SRV3 False 2019-01-01
SRV4 True 2019-01-01
SRV5 False 2019-01-01
SRV1 True 2019-01-02
SRV2 True 2019-01-02
SRV3 False 2019-01-02
SRV4 True 2019-01-02
SRV5 False 2019-01-02
SRV1 True 2019-01-03
SRV2 False 2019-01-03
SRV3 False 2019-01-03
SRV4 True 2019-01-03
SRV5 True 2019-01-03
如果服务器连续30天处于False状态,则我添加一列Alive否:
Server State Date Alive
SRV1 True 2019-01-01 Yes
SRV2 False 2019-01-01 Yes
SRV3 False 2019-01-01 No
SRV4 True 2019-01-01 Yes
SRV5 False 2019-01-01 Yes
SRV1 True 2019-01-02 Yes
SRV2 True 2019-01-02 Yes
SRV3 False 2019-01-02 No
SRV4 True 2019-01-02 Yes
SRV5 False 2019-01-02 Yes
SRV1 True 2019-01-03 Yes
SRV2 False 2019-01-03 Yes
SRV3 False 2019-01-03 No
SRV4 True 2019-01-03 Yes
SRV5 True 2019-01-03 Yes
这里只有SRV3处于非活动状态,因为它的状态连续3天为False。
如何连续30天获得此输出?
答案 0 :(得分:1)
检查此。请注意30天是固定的,并且在查询中使用了两次。如果要在不同的时间段上应用相同的逻辑,则需要在脚本中更改两个静态数字。
SELECT *,
CASE
WHEN (
SELECT COUNT(State)
FROM your_table B
WHERE B.State = 'False'
AND B.Server = A.Server
AND (B.Date BETWEEN DATEADD(DD,-30,A.Date) AND A.Date)
) =30 THEN 'No'
ELSE 'Yes'
END Alive
FROM your_table A
ORDER BY 1,3
答案 1 :(得分:0)
尝试一下,可能有助于解决问题
with open(fig_file, "r") as f:
for line in f:
fields = line.split(" ")
field1 = fields[0]
print(field1)
with open("list.txt","w") as wp:
wp.write(field1)