每天跟踪表格中的更改

时间:2016-06-28 08:43:03

标签: sql sql-server temp

我有一个存储我们每个属性的表,以及他们是否在网站上有相应的图标。该表每日更新并加盖日期。

表格字段:

  • DateStamp,
  • PropertyCode,
  • PropertyName,
  • FacilityBabyFriendly,
  • FacilityDogsAccepted,
  • FacilitySwimmingPool,
  • FacilityHotTub, FacilityInternetAccess,
  • FacilitySeaViews,
  • FacilityParking

所有Facility字段都存储为1 [有图标]或0 [无图标]

我需要的是查询,按属性显示,如果任何设施字段每天都有变化。

如果有人能指出我正确的方向,我会非常感激。

3 个答案:

答案 0 :(得分:0)

一旦你可以做的就是计算每一行的哈希值,以便在有任何变化时进行比较:

select hashbytes('MD5',cast(FacilityBabyFriendly * 1000000 + FacilityDogsAccepted * 100000 + FacilitySwimmingPool * 10000 + FacilityHotTub * 1000 + FacilityInternetAccess * 100 + FacilitySeaViews * 10 + FacilityParking
as varchar(100) )
) from yourtable

答案 1 :(得分:0)

以下查询将为您提供一个属性列表,其中至少有一个设施字段与前一天相比发生了变化

SELECT 
     PropertyCode
    ,PropertyName
FROM Souce s1
WHERE 
    DateStamp = Current_Date
    AND 
    EXISTS
    (SELECT 1 
        FROM 
    Source s2
    WHERE 
        s1.PropertyCode=s2.PropertyCode
        AND DateStamp = Current_Date - 1 Day
        AND 
        (
        (s1.FacilityBabyFriendly<>s2.FacilityBabyFriendly)
        OR 
        (s1.FacilityDogsAccepted<>s2.FacilityDogsAccepted)
        OR
        (s1.FacilitySwimmingPool<>s2.FacilitySwimmingPool)
        OR 
        (s1.FacilityHotTub<>s2.FacilityHotTub)
        OR 
        (s1.FacilityInternetAccess<>s2.FacilityInternetAccess)
        OR 
        (s1.FacilitySeaViews<>s2.FacilitySeaViews)
        OR 
        (s1.FacilityParking<>s2.FacilityParking)
        )
    )

答案 2 :(得分:0)

值&lt;&gt; 0表示设施已更改

SELECT DateStamp
      ,PropertyCode
      ,PropertyName
      ,FacilityBabyFriendly - LAG(FacilityBabyFriendly) OVER (PARTITION BY PropertyCode, PropertyName ORDER BY DateStamp) AS FacilityBabyFriendlyChanged
      ,FacilityDogsAccepted - LAG(FacilityDogsAccepted) OVER (PARTITION BY PropertyCode, PropertyName ORDER BY DateStamp) AS FacilityDogsAcceptedChanged
      ,FacilitySwimmingPool - LAG(FacilitySwimmingPool) OVER (PARTITION BY PropertyCode, PropertyName ORDER BY DateStamp) AS FacilitySwimmingPoolChanged
      ,FacilityHotTub - LAG(FacilityHotTub) OVER (PARTITION BY PropertyCode, PropertyName ORDER BY DateStamp) AS FacilityHotTubChanged
      ,FacilityInternetAccess - LAG(FacilityInternetAccess) OVER (PARTITION BY PropertyCode, PropertyName ORDER BY DateStamp) AS FacilityInternetAccessChanged
      ,FacilitySeaViews - LAG(FacilitySeaViews) OVER (PARTITION BY PropertyCode, PropertyName ORDER BY DateStamp) AS FacilitySeaViewsChanged
      ,FacilityParking - LAG(FacilityParking) OVER (PARTITION BY PropertyCode, PropertyName ORDER BY DateStamp) AS FacilityParkingChanged
FROM YourTable