Advertisement
YuvalGai

Untitled

Jun 20th, 2023 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. CREATE OR REPLACE PROCEDURE CANDIVORE.SEMANTIC_LAYER."SP_INCREMENTAL_Interval_Session_Inventory_Resource_Sub_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_Sub_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_Sub_Type AS(
  30. SELECT
  31. Interval_Date_Time
  32. ,Resource_Type_Id
  33. ,Resource_Type
  34. ,Resource_Sub_Type_Id
  35. ,Resource_Sub_Type
  36. ,Booster_Tier_Group_Id
  37. ,Booster_Tier_Group
  38. ,Resource_Status
  39. ,LTV_Group
  40. ,LTV_Group_30D
  41. ,Seniority_Bin
  42. ,Arena_Group
  43. ,payers_segment
  44. ,TROPHY_GROUP
  45. ,trophy_group_2
  46. ,COUNT(DISTINCT User_Id) as User_Count
  47. ,SUM(Start_Session_Resource_Count) as Start_Session_Resource_Count
  48. FROM SNOWPLOW.DERIVED."v_INTERVAL_SESSION_INVENTORY" I
  49. WHERE TO_DATE(Interval_Date_Time) >= DATEADD(''DAY'',-31,TO_DATE(GETDATE()))
  50. and Interval_Date_Time > (SELECT MAX_Interval_Date_Time FROM TMP_UM_MAX_INSERT_DATE)
  51. GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)`
  52.  
  53.  
  54. var statement2 = snowflake.createStatement({sqlText: my_sql_command2});
  55. statement2.execute();
  56.  
  57.  
  58.  
  59. //3. Insert Delta into Target Table
  60. var my_sql_command3 = "INSERT INTO CANDIVORE.SEMANTIC_LAYER.T_Interval_Session_Inventory_Resource_Sub_Type \\
  61. SELECT * \\
  62. FROM CANDIVORE.SEMANTIC_LAYER.TMP_Interval_Session_Inventory_Resource_Sub_Type;";
  63. var statement3 = snowflake.createStatement({sqlText: my_sql_command3});
  64. statement3.execute();
  65.  
  66. //4. Clean up tables
  67. var my_sql_command4_1 = "DROP TABLE IF EXISTS CANDIVORE.SEMANTIC_LAYER.TMP_Interval_Session_Inventory_Resource_Sub_Type;";
  68. var statement4_1 = snowflake.createStatement({sqlText: my_sql_command4_1});
  69. statement4_1.execute();
  70.  
  71. var my_sql_command4_1 = "DROP TABLE IF EXISTS CANDIVORE.SEMANTIC_LAYER.TMP_UM_MAX_INSERT_DATE;";
  72. var statement4_1 = snowflake.createStatement({sqlText: my_sql_command4_1});
  73. statement4_1.execute();
  74.  
  75. var my_sql_command99 = "COMMIT;";
  76. var statement99 = snowflake.createStatement({sqlText: my_sql_command99});
  77. statement99.execute();
  78. ';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement