Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. SELECT st.user_id,
  2. to_json(max(case when id=maxid then properties->'assumed_gender' end)) AS assumed_gender,
  3. to_json(max(case when id=maxid then properties->'email' end)) AS email,
  4. to_json(max(case when id=maxid then properties->'first_name' end)) AS first_name,
  5. to_json(max(case when id=maxid then properties->'last_name' end)) AS last_name,
  6. to_json(max(case when id=maxid then properties->'country_name' end)) AS country_name,
  7. to_json(max(case when id=maxid then properties->'city_name' end)) AS city_name,
  8. to_json(max(case when id=maxid then properties->'mobile_number' end)) AS mobile_number,
  9. json_agg(to_json(properties->'submission_url')) AS submission_urls
  10. FROM daily_statistics st JOIN (
  11. SELECT u.id as user_id, (
  12. SELECT st2.id FROM daily_statistics st2 WHERE st2.user_id=u.id
  13. ORDER BY st2.id DESC LIMIT 1) AS maxid FROM users u
  14. )
  15. mu ON (st.user_id=mu.user_id)
  16. GROUP BY st.user_id;
  17.  
  18. DailyStatistic.select("
  19. daily_statistics.user_id,
  20. to_json(max(case when id=maxid then properties->'following' end)),
  21. to_json(max(case when id=maxid then properties->'assumed_gender' end)),
  22. to_json(max(case when id=maxid then properties->'email' end)),
  23. to_json(max(case when id=maxid then properties->'first_name' end)),
  24. to_json(max(case when id=maxid then properties->'last_name' end)),
  25. to_json(max(case when id=maxid then properties->'country_name' end)),
  26. to_json(max(case when id=maxid then properties->'city_name' end)),
  27. to_json(max(case when id=maxid then properties->'mobile_number' end)),
  28. json_agg(to_json(properties->'submission_url'))
  29. ").join("
  30. JOIN(
  31. SELECT u.id as user_id, (
  32. SELECT st2.id FROM daily_statistics st2 WHERE st2.user_id=u.id
  33. ORDER BY st2.id DESC LIMIT 1) AS maxid FROM users u
  34. )
  35. mu ON (daily_statistics.user_id=mu.user_id)
  36. ").group("daily_statistics.user_id")
  37.  
  38. PG::UndefinedColumn: ERROR: column "maxid" does not exist
  39. LINE 1: ...aily_statistics.user_id, to_json(max(case when id=maxid then...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement