在我的环境中,我们使用Altiris来控制我们的资产,每天我们制定了一项策略,将退役状态设置为离线超过45天的计算机,如果此计算机在网络上联机,则返回活动状态。
当策略更改它在数据库两行上写入的状态时,问题有时(并非适用于所有设备): 首先是当前状态,另一个是新状态:
当设备返回活动状态时也会出现同样的情况,因此当我尝试使用SUM数据来了解有多少设备已按月更改为已退役或活动时,这个数字没有意义,因为对于某些设备我们有两条线路2同一dataChanged上的不同状态
例如:
ComputerName Date Changed Status
001PROJNEW-VM 13/01/2015 17:33 Active
002PROJNEW-VM 11/09/2014 11:58 Retired
002PROJNEW-VM 07/10/2014 21:10 Retired
002PROJNEW-VM 07/10/2014 21:10 Active
003PROJNEW-VM 11/09/2014 11:58 Retired
003PROJNEW-VM 13/11/2014 03:27 Retired
003PROJNEW-VM 13/11/2014 03:27 Active
004PROJNEW-VM 06/04/2015 20:00 Retired
005PROJNEW-VM 11/09/2014 11:58 Retired
005PROJNEW-VM 09/10/2014 21:09 Retired
005PROJNEW-VM 09/10/2014 21:09 Active
005PROJNEW-VM 06/04/2015 20:00 Retired
006PROJNEW-VM 26/12/2014 20:00 Retired
006PROJNEW-VM 31/12/2014 05:34 Retired
006PROJNEW-VM 31/12/2014 05:34 Active
006PROJNEW-VM 06/01/2015 20:00 Retired
007PROJNEW-VM 11/09/2014 11:58 Retired
007PROJNEW-VM 27/12/2014 05:38 Retired
007PROJNEW-VM 27/12/2014 05:38 Active
007PROJNEW-VM 12/04/2015 19:50 Retired
008PROJNEW-VM 11/09/2014 11:58 Retired
008PROJNEW-VM 29/10/2014 05:44 Retired
008PROJNEW-VM 29/10/2014 05:44 Active
008PROJNEW-VM 06/04/2015 20:00 Retired
009PROJNEW-VM 11/09/2014 11:58 Retired
009PROJNEW-VM 17/09/2014 20:33 Retired
009PROJNEW-VM 17/09/2014 20:33 Active
009PROJNEW-VM 19/02/2015 20:00 Retired
010PROJNEW-VM 11/09/2014 11:58 Retired
010PROJNEW-VM 29/10/2014 05:44 Retired
010PROJNEW-VM 29/10/2014 05:44 Active
010PROJNEW-VM 06/04/2015 20:00 Retired
011PROJNEW-VM 05/04/2015 20:00 Retired
013PROJNEW-VM 20/02/2015 20:00 Retired
014PROJNEW-VM 06/04/2015 20:00 Retired
基本上我需要的,实际上我不能做的是:如果主机名有两个相等的'Date Changed'和两个不同的'status'带来查询结果,该主机名的最后一行和'Date Changed'...
结果例如:
Nome do computador Date Changed Status
001PROJNEW-VM 13/01/2015 17:33 Active
002PROJNEW-VM 11/09/2014 11:58 Retired
002PROJNEW-VM 07/10/2014 21:10 Active
003PROJNEW-VM 11/09/2014 11:58 Retired
003PROJNEW-VM 13/11/2014 03:27 Active
004PROJNEW-VM 06/04/2015 20:00 Retired
005PROJNEW-VM 11/09/2014 11:58 Retired
005PROJNEW-VM 09/10/2014 21:09 Active
005PROJNEW-VM 06/04/2015 20:00 Retired
006PROJNEW-VM 26/12/2014 20:00 Retired
006PROJNEW-VM 31/12/2014 05:34 Active
006PROJNEW-VM 06/01/2015 20:00 Retired
007PROJNEW-VM 11/09/2014 11:58 Retired
007PROJNEW-VM 27/12/2014 05:38 Active
007PROJNEW-VM 12/04/2015 19:50 Retired
008PROJNEW-VM 11/09/2014 11:58 Retired
008PROJNEW-VM 29/10/2014 05:44 Active
008PROJNEW-VM 06/04/2015 20:00 Retired
009PROJNEW-VM 11/09/2014 11:58 Retired
009PROJNEW-VM 17/09/2014 20:33 Active
009PROJNEW-VM 19/02/2015 20:00 Retired
010PROJNEW-VM 11/09/2014 11:58 Retired
010PROJNEW-VM 29/10/2014 05:44 Active
010PROJNEW-VM 06/04/2015 20:00 Retired
011PROJNEW-VM 05/04/2015 20:00 Retired
013PROJNEW-VM 20/02/2015 20:00 Retired
014PROJNEW-VM 06/04/2015 20:00 Retired
答案 0 :(得分:1)
假设您有一个指定排序的id
列,那么您可以使用row_number()
:
select t.*
from (select t.*,
row_number() over (partition by computername, datechanged
order by id desc) as seqnum
from table t
) t
where seqnum = 1;
在您的特定示例中,所有重复项似乎都处于活动状态。如果是这种情况,那么:
select computername, datechanged,
(case when min(status) = max(status) then min(status)
when sum(case when status = 'Active' then 1 else 0 end) > 0
then 'Active'
else '***Unknown***'
end) as status
from table t
group by computername, datechanged;