Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH
  2.     se AS (
  3.     SELECT
  4.         se.student_id,
  5.         se.school_id,
  6.         se.grade_id
  7.  
  8.     FROM
  9.         student_enrollment se
  10.  
  11.     WHERE
  12.         se.syear=2015
  13.         AND (se.end_date IS NULL OR se.end_date >= current_date)
  14.         AND se.custom_9 IS NOT NULL
  15.     ),
  16.     totalbridge AS (
  17.         SELECT
  18.             current_date::date,
  19.             sg.short_name
  20.  
  21.         FROM
  22.             school_gradelevels sg
  23.     ),
  24.     total AS (
  25.         SELECT
  26.             current_date::date,
  27.             count(*) AS TOTAL
  28.  
  29.         FROM
  30.             students s
  31.             JOIN se ON (s.student_id=se.student_id)
  32.     )
  33.  
  34. SELECT
  35.     current_date::date,
  36.     sc.custom_327 AS SCHOOL_ID,
  37.     sg.short_name AS GRADE_LEVEL,
  38.     COALESCE(SUM(CASE
  39.         WHEN s.custom_200000001 = 'Asian or Pacific Islander' THEN 1
  40.         ELSE 0
  41.     END),0) AS ASIAN,
  42.     COALESCE(SUM(CASE
  43.         WHEN s.custom_200000001 = 'Black, Non-Hispanic' THEN 1
  44.         ELSE 0
  45.     END),0) AS BLACK,
  46.     COALESCE(SUM(CASE
  47.         WHEN s.custom_200000001 = 'Hispanic' THEN 1
  48.         ELSE 0
  49.     END),0) AS HISPANIC,
  50.     COALESCE(SUM(CASE
  51.         WHEN s.custom_200000001 = 'Amer. Indian or Alaskan Native' THEN 1
  52.         ELSE 0
  53.     END),0) AS INDN,
  54.     COALESCE(SUM(CASE
  55.         WHEN s.custom_200000001 = 'Other' THEN 1
  56.         ELSE 0
  57.     END),0) AS MULTIR,
  58.     COALESCE(SUM(CASE
  59.         WHEN s.custom_200000001 = 'White, Non-Hispanic' THEN 1
  60.         ELSE 0
  61.     END),0) AS WHITE,
  62.     COALESCE(SUM(CASE
  63.         WHEN s.custom_200000000 = 'F - Female' THEN 1
  64.         ELSE 0
  65.     END),0) AS FEMALE,
  66.     COALESCE(SUM(CASE
  67.         WHEN s.custom_200000000 = 'M - Male' THEN 1
  68.         ELSE 0
  69.     END),0) AS MALE,
  70.     count(*) AS TOTAL,
  71.     count(*) / total.total AS "%"
  72.  
  73. FROM
  74.     students s
  75.     JOIN se ON (s.student_id=se.student_id)
  76.     JOIN schools sc ON (se.school_id=sc.id)
  77.     JOIN school_gradelevels sg ON (se.grade_id=sg.id)
  78.     JOIN totalbridge ON (sg.short_name=totalbridge.short_name)
  79.     JOIN total ON (totalbridge.date=total.date)
  80.  
  81. GROUP BY
  82.     1,2,3,total.total
  83.  
  84. UNION
  85.  
  86. SELECT
  87.     current_date::date,
  88.     'TOTAL',
  89.     'TOTAL',
  90.     COALESCE(SUM(CASE
  91.         WHEN s.custom_200000001 = 'Asian or Pacific Islander' THEN 1
  92.         ELSE 0
  93.     END),0) AS ASIAN,
  94.     COALESCE(SUM(CASE
  95.         WHEN s.custom_200000001 = 'Black, Non-Hispanic' THEN 1
  96.         ELSE 0
  97.     END),0) AS BLACK,
  98.     COALESCE(SUM(CASE
  99.         WHEN s.custom_100000105 = 'Yes' THEN 1
  100.         ELSE 0
  101.     END),0) AS HISPANIC,
  102.     COALESCE(SUM(CASE
  103.         WHEN s.custom_200000001 = 'Amer. Indian or Alaskan Native' THEN 1
  104.         ELSE 0
  105.     END),0) AS INDN,
  106.     COALESCE(SUM(CASE
  107.         WHEN s.custom_200000001 = 'Other' THEN 1
  108.         ELSE 0
  109.     END),0) AS MULTIR,
  110.     COALESCE(SUM(CASE
  111.         WHEN s.custom_200000001 = 'White, Non-Hispanic' THEN 1
  112.         ELSE 0
  113.     END),0) AS WHITE,
  114.     COALESCE(SUM(CASE
  115.         WHEN s.custom_200000000 = 'F - Female' THEN 1
  116.         ELSE 0
  117.     END),0) AS FEMALE,
  118.     COALESCE(SUM(CASE
  119.         WHEN s.custom_200000000 = 'M - Male' THEN 1
  120.         ELSE 0
  121.     END),0) AS MALE,
  122.     count(*) AS TOTAL,
  123.     count(*) / total.total AS "%"
  124.  
  125. FROM
  126.     students s
  127.     JOIN se ON (s.student_id=se.student_id)
  128.     JOIN school_gradelevels sg ON (se.grade_id=sg.id)
  129.     JOIN totalbridge ON (sg.short_name=totalbridge.short_name)
  130.     JOIN total ON (totalbridge.date=total.date)
  131.  
  132. GROUP BY
  133.     1,2,3,total.total
  134.  
  135. ORDER BY
  136.     "%",1,2,3;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement