0

I have a table that stores each of our properties and whether they have certain icons against them on the website. This table is updated on a daily basis and date stamped.

Table Fields:

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

All the Facility fields are stored as a 1 [has icon] or 0 [no icon]

What I need is query that shows me, by property, if any of the facility fields have changed on a daily basis.

If anyone could point me in the right direction, if would be much appreciated.

glennsl
  • 28,186
  • 12
  • 57
  • 75
Claire
  • 51
  • 6

3 Answers3

0

once thing you can do is calculate the hash of each row to compare if there is any change:

select hashbytes('MD5',cast(FacilityBabyFriendly * 1000000 + FacilityDogsAccepted * 100000 + FacilitySwimmingPool * 10000 + FacilityHotTub * 1000 + FacilityInternetAccess * 100 + FacilitySeaViews * 10 + FacilityParking
as varchar(100) )
) from yourtable
Jayvee
  • 10,670
  • 3
  • 29
  • 40
0

The following query will give you the list of properties which has atleast one of the facility fields changed as compared with the previous day

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)
        )
    )
Srini
  • 178
  • 1
  • 6
0

Value <> 0 means facility has changed

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
adrianm
  • 14,468
  • 5
  • 55
  • 102