Last active
          June 23, 2016 20:21 
        
      - 
      
- 
        Save spencerroan/af2673fca4f4fa92e9a9e5964f85cc9a to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | with hydrated_sleeps as ( | |
| select ss.user_id, ss.well_rested, ss.occurred_on, u.company_id, u.gender | |
| from simple_sleeps ss inner join users u on u.id = ss.user_id | |
| ), | |
| filtered_sleeps as ( | |
| select * from hydrated_sleeps where company_id = 12 or company_id = 4 | |
| ), | |
| sleep_counts as ( | |
| select | |
| occurred_on, | |
| sum(case when well_rested=false then 1 else 0 end) not_well_rested, | |
| sum(case when well_rested=true then 1 else 0 end) well_rested, | |
| count(occurred_on) total | |
| from filtered_sleeps | |
| group by occurred_on | |
| order by occurred_on | |
| ) | |
| select | |
| occurred_on, | |
| round((100 * not_well_rested::float/total)::numeric, 2) not_well_rested, | |
| round((100 * well_rested::float/total)::numeric, 2) well_rested, | |
| well_rested well_rested_count, | |
| not_well_rested not_well_rested_count | |
| from sleep_counts | |
| ; | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment