Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. select c.data into data from doc c where c.doc_id = id and c.group_cur > group_cur order by c.id desc limit 1;
  2. EXCEPTION
  3. WHEN NO_DATA_FOUND THEN
  4. select c.data into data from doc c where c.doc_id = id and c.global_cur > global_cur order by c.id desc limit 1;
  5. EXCEPTION
  6. WHEN NO_DATA_FOUND THEN
  7. RETURN NULL;
  8.  
  9. SELECT c.data into data
  10. FROM doc c
  11. WHERE c.doc_id = id
  12. and (
  13. c.group_cur > group_cur
  14. or
  15. c.global_cur > global_cur
  16. )
  17. ORDER BY
  18. -- this will make group always preferred over global
  19. case when c.group_cur > group_cur then 1 else 2 end ASC,
  20. -- and this is your normal ordering
  21. c.id DESC
  22. limit 1;
  23.  
  24. select c.data into data
  25. from doc c
  26. where c.doc_id = id and c.group_cur > group_cur
  27. order by c.id desc limit 1;
  28. if not found then
  29. select c.data into data
  30. from doc c
  31. where c.doc_id = id and c.global_cur > global_cur
  32. order by c.id desc limit 1;
  33. if not found then return null; end if;
  34. end if;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement