简体   繁体   中英

Tracking changes in a SQL database

I've spent plenty of time Googling and have found possible solutions to much simpler tables.

My company uses BGInfo religiously to keep track of ~80 properties on over 200 web servers. Those fields are a mix of server stats (IP address, OSVer, HyperV host) and versions of various installed software components. A scheduled task writes this information every day all web servers to a single database (currently >300K records). We need a query (to eventually be fed into a report) to give us a time of when something has changed on any given web server. A sort of automated change control if you will.

Example: WebSvr_XYZ used had 2G of RAM since inception before getting additional RAM allocated a few months later. Then a year after that, it was given a new IP address.

Server      Time_stamp      Host    IP              RAM
-------------------------------------------------------
WebSvr_XYZ  June 1, 2016    Virt5a  192.168.10.45   2G
WebSvr_XYZ  June 2, 2016    Virt5a  192.168.10.45   2G
WebSvr_XYZ  Aug 20, 2016    Virt5a  192.168.10.45   4G
WebSvr_XYZ  Aug 21, 2016    Virt5a  192.168.10.45   4G
WebSvr_XYZ  July 18, 2017   Virt5a  192.168.20.105  4G
WebSvr_XYZ  July 19, 2017   Virt5a  192.168.20.105  4G
WebSvr_XYZ  July 20, 2017   Virt5a  192.168.20.105  4G

When running against WebSvr_XYZ , the output against over 540 records would be

June 1, 2016    Virt5a  192.168.10.45   2G
Aug 20, 2016    Virt5a  192.168.10.45   4G
July 18, 2017   Virt5a  192.168.20.105  4G

I've tried the select distinct against the table them Joining it against the full table, joining on all relevant columns and using a MIN(Timestamp) to get the first. But I either get bad timestamps or no results at all.

use row_number() as a sequence of event. After that you can INNER JOIN back itself data of comparing previous timestamp with current timestamp

; with cte as
(
    select *, rn = row_number() over (partition by Server order by Time_stamp)
    from   yourtable
)
select *
from   cte c1
       inner join c2 on  c1.Server = c2.Server
                     and c1.rn = c2.rn - 1
where c1.IP  <> c2.IP
or    c1.RAM <> c2.RAM

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM