Advertisement
AtharvaSune99

Final-PostgreSQL-file

Nov 20th, 2022 (edited)
1,928
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- some data cleaning on the table
  2. --
  3. -- in the comchannel column in some cells, SMS has been shortened to S
  4. -- Email has been shortened to E
  5. -- and Phone has been shortened to P
  6. -- this update block updates them to their complete values
  7. UPDATE customer
  8. SET comchannel = CASE
  9.         WHEN comchannel = 'S' THEN 'SMS'
  10.         WHEN comchannel = 'E' THEN 'Email'
  11.         WHEN comchannel = 'P' THEN 'Phone'
  12.         else comchannel
  13.     END;
  14. -- in the gender column in some cells, female has been shortened to f
  15. -- and male has been shortened to m. This update block updates them
  16. -- to their full values
  17. UPDATE customer
  18. SET Gender = CASE
  19.         WHEN Gender = 'f' THEN 'female'
  20.         WHEN Gender = 'm' THEN 'male'
  21.         else Gender
  22.     END;
  23. -- create analytics base table
  24. -- c is an alias for customer table
  25. -- mp is an alias for motor_policies table
  26. -- tp is an alias for travel_policies table
  27. -- hp is an alias for health_policies table
  28. -- alias is used because some column names are same
  29. -- across the table and selecting those column by referencing
  30. -- using alias helps avoid any conflicts
  31. CREATE TABLE IF NOT EXISTS abt AS
  32. SELECT c.customerid,
  33.     c.givenname,
  34.     c.surname,
  35.     c.Gender,
  36.     c.CardType,
  37.     c.location,
  38.     c.comchannel,
  39.     c.motorid,
  40.     mp.MotorType,
  41.     mp.policystart AS mp_policystart,
  42.     mp.policyend AS mp_policyend,
  43.     c.healthid,
  44.     hp.HealthType,
  45.     hp.policystart AS hp_policystart,
  46.     hp.policyend AS hp_policyend,
  47.     c.travelid,
  48.     tp.TravelType,
  49.     tp.policystart AS tp_policystart,
  50.     tp.policyend AS tp_policyend
  51.     FROM customer c
  52.     LEFT JOIN motor_policies mp ON c.motorid = mp.motorid
  53.     LEFT JOIN health_policies hp ON c.healthid = hp.healthid
  54.     LEFT JOIN travel_policies tp ON c.travelid = tp.travelid;
  55. -- data analysis on the abt table, which groups the table
  56. -- by Location and comchannel and gives the count for each
  57. -- group
  58. SELECT abt.location,
  59.     comchannel,
  60.     count(customerid)
  61. FROM abt
  62. GROUP BY abt.location,
  63.     comchannel
  64. ORDER BY abt.location,
  65.     comchannel;
  66. -- data analysis on the abt table, which groups the table
  67. -- by Gender and comchannel and gives the count for each
  68. -- group
  69. SELECT Gender,
  70.     comchannel,
  71.     count(customerid)
  72. FROM abt
  73. GROUP BY Gender,
  74.     comchannel
  75. ORDER BY Gender,
  76.     comchannel;
  77. -- data analysis on the abt table, which groups the table
  78. -- by CardType and comchannel and gives the count for each
  79. -- group
  80. SELECT CardType,
  81.     comchannel,
  82.     count(customerid)
  83. FROM abt
  84. GROUP BY CardType,
  85.     comchannel
  86. ORDER BY CardType,
  87.     comchannel;
  88. -- data analysis on the abt table, which groups the table
  89. -- by CardType and motorpolicy type and gives the count for each
  90. -- group
  91. SELECT CardType,
  92.     MotorType,
  93.     count(customerid)
  94. FROM abt
  95. GROUP BY CardType,
  96.     MotorType
  97.     ORDER BY CardType,
  98.     MotorType;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement