daily pastebin goal
47%
SHARE
TWEET

Untitled

recroot89 Mar 21st, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. User
  2.   .joins(:clients)
  3.   .joins('LEFT JOIN positions ON positions.client_id = clients.id')
  4.   .select('users.*, SUM(CASE users.id WHEN clients.manager_id THEN 1 ELSE 0 END) as clients_count, '\
  5.           'SUM(CASE positions.status WHEN 0 THEN 1 ELSE 0 END) as active_positions_count, '\
  6.           'SUM(CASE positions.status WHEN 1 THEN 1 ELSE 0 END) as passive_positions_count')
  7.   .having('SUM(CASE positions.status WHEN 0 THEN 1 ELSE 0 END) > 0 '\
  8.           'OR SUM(CASE positions.status WHEN 1 THEN 1 ELSE 0 END) > 0')
  9.   .group('users.id')
  10.  
  11. User
  12.   .joins('INNER JOIN ('\
  13.           'SELECT '\
  14.             'clients.id, '\
  15.             'clients.manager_id, '\
  16.             'SUM(CASE WHEN positions.status = 0 THEN 1 ELSE 0 END) AS active_positions_count, '\
  17.             'SUM(CASE WHEN positions.status = 1 THEN 1 ELSE 0 END) AS passive_positions_count '\
  18.           'FROM clients '\
  19.           'INNER JOIN '\
  20.             'positions ON positions.client_id = clients.id AND positions.status IN (0, 1) '\
  21.           'GROUP BY clients.id'\
  22.         ') AS sums ON sums.manager_id = users.id')
  23.   .select('users.*, '\
  24.           'COUNT(sums.manager_id) AS clients_count, '\
  25.           'CAST(SUM(sums.active_positions_count) AS BIGINT) AS active_positions_count, '\
  26.           'CAST(SUM(sums.passive_positions_count) AS BIGINT) AS passive_positions_count')
  27.   .group('users.id')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top