Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. --Script for Loading HRVHS Patrons and adding Freshman/Grad Year Stat Cats--
  2.  
  3. BEGIN;
  4.  
  5. --DROP TABLE IF EXISTS hrvhs_freshman_load;
  6. --DROP TABLE IF EXISTS hrvhs_freshman_load_ids;
  7. --DROP TABLE IF EXISTS hrvhs_stat_cat_1;
  8. --DROP TABLE IF EXISTS hrvhs_stat_cat_2;
  9.  
  10.  
  11. -- Hints --
  12. -- test db perm group = 16, org_unit = 246
  13. -- statcat1 = Class Standing
  14. -- statcat2 = Grad Year
  15.  
  16. --Create main student staging table.
  17. CREATE TABLE hrvhs_freshman_load (
  18. h_studentid text, -- Student ID # // ident_value
  19. h_usrname text, -- will concat 'hrv+studentid' later
  20. h_first_name text,
  21. h_middle_name text,
  22. h_last_name text,
  23. h_dob date,
  24. -- h_day_phone
  25. h_home_ou int DEFAULT 214, -- Hood River High School = 246
  26. h_profile int DEFAULT 90, -- STUDENT - HIGH SCHOOL
  27. h_ident_type int DEFAULT 2, -- Student ID
  28. h_net_access_level int DEFAULT 2,
  29. h_passwd text, -- student id
  30. --
  31. h_address_type text,
  32. h_street1 text,
  33. h_street2 text,
  34. h_city text,
  35. h_state text,
  36. h_country text,
  37. h_postal_code text,
  38. --
  39. h_statcat1 text,
  40. h_statcat2 text
  41. );
  42.  
  43. --Copy records from your import text file
  44. COPY hrvhs_freshman_load (
  45. h_studentid, -- Student ID
  46. h_first_name,
  47. h_middle_name,
  48. h_last_name,
  49. h_dob,
  50. --h_dayphone,
  51. --
  52. h_address_type,
  53. h_street1,
  54. h_street2,
  55. h_city,
  56. h_state,
  57. h_country,
  58. h_postal_code,
  59. --
  60. h_statcat1,
  61. h_statcat2
  62. )
  63. FROM '/home/bmills/hrvhs_freshman_load.csv' WITH CSV HEADER;
  64.  
  65. --Create the usrname(barcode) entry from the student id (ident_value)
  66. UPDATE hrvhs_freshman_load SET h_usrname='hrv'||h_studentid;
  67.  
  68. -- Set password=student ID for now
  69. UPDATE hrvhs_freshman_load SET h_passwd=h_studentid;
  70.  
  71. --Insert records from the hrvhs_freshman_load table into the actor.usr table.
  72. INSERT INTO actor.usr (
  73. profile,
  74. usrname,
  75. passwd,
  76. ident_type,
  77. ident_value,
  78. first_given_name,
  79. family_name,
  80. home_ou,
  81. dob,
  82. -- day_phone,
  83. net_access_level)
  84. SELECT
  85. h_profile,
  86. h_usrname,
  87. h_passwd,
  88. h_ident_type,
  89. h_studentid,
  90. h_first_name,
  91. h_last_name,
  92. h_home_ou,
  93. h_dob,
  94. -- h_dayphone,
  95. h_net_access_level
  96. FROM hrvhs_freshman_load;
  97.  
  98. -- Make our lives easier and create a table of actor.usr ids of the incoming students for later consulation
  99. CREATE TABLE hrvhs_freshman_load_ids(
  100. h_actor_usr_id int,
  101. h_usrname text,
  102. h_studentid text,
  103. h_create_time date DEFAULT now(),
  104. h_class_and_year text DEFAULT '2020 - Freshman'
  105. );
  106. -- Load it up
  107. INSERT INTO hrvhs_freshman_load_ids(h_actor_usr_id,h_usrname,h_studentid)
  108. SELECT au.id, hrvhs.h_usrname,hrvhs.h_studentid
  109. FROM hrvhs_freshman_load hrvhs
  110. INNER JOIN actor.usr au
  111. ON hrvhs.h_usrname=au.usrname;
  112.  
  113. --Load barcodes into actor.card table from the usrname entries.
  114. INSERT INTO actor.card (usr, barcode)
  115. SELECT h_actor_usr_id,h_usrname
  116. FROM hrvhs_freshman_load_ids;
  117.  
  118. --Update actor.usr.card field with actor.card.id to associate active card with the user:
  119. --Play it safe and only update when User ID is in the hrvhs id table
  120. UPDATE actor.usr au
  121. SET card = ac.id
  122. FROM actor.card ac
  123. WHERE ac.usr = au.id AND au.id IN (SELECT h_actor_usr_id FROM hrvhs_freshman_load_ids);
  124.  
  125. --Update actor.usr_address from staging table.
  126. INSERT INTO actor.usr_address (usr, street1, street2, city, state, country, post_code)
  127. SELECT au.id,
  128. hrvhs.h_street1,
  129. hrvhs.h_street2,
  130. hrvhs.h_city,
  131. hrvhs.h_state,
  132. hrvhs.h_country,
  133. hrvhs.h_postal_code
  134. FROM hrvhs_freshman_load hrvhs
  135. INNER JOIN actor.usr au ON au.usrname=hrvhs.h_usrname;
  136.  
  137.  
  138. --Update actor.usr mailing address with id from actor.usr_address table:
  139. UPDATE actor.usr au
  140. SET mailing_address = aua.id, billing_address = aua.id
  141. FROM actor.usr_address aua
  142. WHERE au.id = aua.usr
  143. AND au.id IN (SELECT h_actor_usr_id FROM hrvhs_freshman_load_ids);
  144.  
  145. --Insert Stat Cats (Freshman, 2020 Grad Date) -- "Freshman,43 | 2020,69"
  146. --80 (Grad Year), 81 (Class) on test server
  147.  
  148. -- Create Scratch Tables for load
  149. -- Since I know of no nice way to do multiple inserts with one value dynamically changing
  150.  
  151. --School Level--
  152. CREATE TABLE hrvhs_stat_cat_1 (
  153. stat_cat_entry text DEFAULT 'Freshman',
  154. stat_cat int DEFAULT 43,
  155. target_usr int
  156. );
  157. INSERT INTO hrvhs_stat_cat_1 (target_usr)
  158. SELECT h_actor_usr_id FROM hrvhs_freshman_load_ids;
  159.  
  160. INSERT INTO actor.stat_cat_entry_usr_map (stat_cat_entry,stat_cat,target_usr)
  161. SELECT stat_cat_entry,stat_cat,target_usr FROM hrvhs_stat_cat_1;
  162.  
  163. --Grad Year--
  164. CREATE TABLE hrvhs_stat_cat_2 (
  165. stat_cat_entry text DEFAULT '2020',
  166. stat_cat int DEFAULT 69,
  167. target_usr int
  168. );
  169. INSERT INTO hrvhs_stat_cat_2 (target_usr)
  170. SELECT h_actor_usr_id FROM hrvhs_freshman_load_ids;
  171.  
  172. INSERT INTO actor.stat_cat_entry_usr_map (stat_cat_entry,stat_cat,target_usr)
  173. SELECT stat_cat_entry,stat_cat,target_usr FROM hrvhs_stat_cat_2;
  174.  
  175. -- DONE! --
  176.  
  177. COMMIT;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement