Skip to content

Instantly share code, notes, and snippets.

@alcheng10
Created March 11, 2020 10:59
Show Gist options
  • Save alcheng10/73239bf20bc81b05ee6886a5f21677df to your computer and use it in GitHub Desktop.
Save alcheng10/73239bf20bc81b05ee6886a5f21677df to your computer and use it in GitHub Desktop.
Date_Daily_Island_with_Rank example
# Example with Dense Rank!
WITH RANKED_DATA AS (
SELECT 
  FullDate
  ,RegistrationGUID
  ,RegistrationNo
  ,PolicyNo
  ,Cover_YN
  ,DENSE_RANK () OVER (
    PARTITION BY 
      RegistrationNo
      ,Cover_YN
    ORDER BY FullDate ASC) 
  AS DateRank
FROM DimCustomer_Car_Status_by_day
)
SELECT 
  FullDate
  ,RegistrationGUID
  ,RegistrationNo
  ,PolicyNo
  ,Cover_YN
  ,DateRank
  --Now get the partition start date by substracting the Dense Rank - 1
  ,DATE(FullDate, '-' || (DateRank - 1) || ' DAYS') AS PartitionStartDate
FROM RANKED_DATA
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment