Advertisement
YuvalGai

Untitled

Jun 20th, 2023 (edited)
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. CREATE OR REPLACE PROCEDURE CANDIVORE.SEMANTIC_LAYER."SP_INCREMENTAL_Interval_Session_Inventory_Resource_Type"()
  2. RETURNS VARCHAR(16777216)
  3. LANGUAGE JAVASCRIPT
  4. EXECUTE AS OWNER
  5. AS '
  6. /*
  7. This script merged the Interval_Session_Inventory_Resource_Name data from the client and the server
  8. The steps are:
  9. 1. Get last insert date
  10. 2. Load the merged data into a temp table, using the following logic:
  11. a. Take rows that have Insert_Date >= Max_Insert_Date we got in #1 to get the smallest delta
  12. b. Merge the data between the two sources
  13. c. Filter out any rows that already exist in target (due to the "=" in the dates)
  14. 3. Insert the content of the temp table into the traget table
  15. 4. Cleanup temp tables created by this script
  16. */
  17. var my_sql_command0 = "BEGIN TRANSACTION;"
  18. var statement0 = snowflake.createStatement({sqlText: my_sql_command0});
  19. statement0.execute();
  20.  
  21. //1. Get last insert date for each source
  22. var my_sql_command1 = `CREATE OR REPLACE TABLE CANDIVORE.SEMANTIC_LAYER.TMP_UM_MAX_INSERT_DATE AS \\
  23. SELECT MAX(Interval_Date_Time) as MAX_Interval_Date_Time \\
  24. FROM CANDIVORE.SEMANTIC_LAYER.T_Interval_Session_Inventory_Resource_Type;`
  25. var statement1 = snowflake.createStatement({sqlText: my_sql_command1});
  26. statement1.execute();
  27.  
  28. //2. Get joined data that has been inserted since the last insert date in the merged table
  29. var my_sql_command2 = `CREATE OR REPLACE TABLE CANDIVORE.SEMANTIC_LAYER.TMP_Interval_Session_Inventory_Resource_Type
  30. AS(
  31. SELECT
  32. Interval_Date_Time
  33. ,Resource_Type_Id
  34. ,case when Resource_Type = ''OnFire'' then resource_name_id else Resource_Type end Resource_Type
  35. -- ,Resource_Sub_Type_Id
  36. -- ,Resource_Sub_Type
  37. -- ,Booster_Tier_Group_Id
  38. -- ,Booster_Tier_Group
  39. ,Resource_Status
  40. ,LTV_Group
  41. ,LTV_Group_30D
  42. ,Seniority_Bin
  43. ,Arena_Group
  44. ,payers_segment
  45. ,TROPHY_GROUP
  46. ,trophy_group_2
  47. ,COUNT(DISTINCT User_Id) as User_Count
  48. ,SUM(Start_Session_Resource_Count) as Start_Session_Resource_Count
  49. FROM SNOWPLOW.DERIVED."v_INTERVAL_SESSION_INVENTORY" I
  50. WHERE TO_DATE(Interval_Date_Time) >= DATEADD(''DAY'',-31,TO_DATE(GETDATE()))
  51. and Interval_Date_Time > (SELECT MAX_Interval_Date_Time FROM TMP_UM_MAX_INSERT_DATE)
  52. GROUP BY 1,2,3,4,5,6,7,8,9,10,11)`
  53.  
  54.  
  55. var statement2 = snowflake.createStatement({sqlText: my_sql_command2});
  56. statement2.execute();
  57.  
  58.  
  59.  
  60. //3. Insert Delta into Target Table
  61. var my_sql_command3 = "INSERT INTO CANDIVORE.SEMANTIC_LAYER.T_Interval_Session_Inventory_Resource_Type \\
  62. SELECT * \\
  63. FROM CANDIVORE.SEMANTIC_LAYER.TMP_Interval_Session_Inventory_Resource_Type;";
  64. var statement3 = snowflake.createStatement({sqlText: my_sql_command3});
  65. statement3.execute();
  66.  
  67. //4. Clean up tables
  68. var my_sql_command4_1 = "DROP TABLE IF EXISTS CANDIVORE.SEMANTIC_LAYER.TMP_Interval_Session_Inventory_Resource_Type;";
  69. var statement4_1 = snowflake.createStatement({sqlText: my_sql_command4_1});
  70. statement4_1.execute();
  71.  
  72. var my_sql_command4_1 = "DROP TABLE IF EXISTS CANDIVORE.SEMANTIC_LAYER.TMP_UM_MAX_INSERT_DATE;";
  73. var statement4_1 = snowflake.createStatement({sqlText: my_sql_command4_1});
  74. statement4_1.execute();
  75.  
  76. var my_sql_command99 = "COMMIT;";
  77. var statement99 = snowflake.createStatement({sqlText: my_sql_command99});
  78. statement99.execute();
  79. ';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement