Guest User

Untitled

a guest
Jan 20th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. SELECT stuff_id.Id, stuff_id.name,
  2. (select top 1 stuff_phone.phone from stuff_phone where stuffid = stuff_id.Id
  3. order by stuff_phone.preference) phone
  4. FROM stuff_id
  5.  
  6. SELECT stuff_id.Id, stuff_id.name,
  7. (select stuff_phone.phone from stuff_phone where stuffid = stuff_id.Id
  8. order by stuff_phone.preference
  9. limit 1) phone
  10. FROM stuff_id
  11.  
  12. SELECT st.Id, st.name, coalesce(max(ph1.phone),max(ph2.phone)) as phone
  13. FROM stuff as st
  14. LEFT JOIN stuff_phone ph1 ON st.id = ph1.id and ph1.preference='p'
  15. LEFT JOIN stuff_phone ph2 ON st.id = ph2.id and ph2.preference!='p'
  16. GROUP BY st.Id, st.name
  17.  
  18. SELECT st.Id, st.name,
  19. coalesce(max(case when ph1.preference='p' then ph1.phone end),
  20. max(ph1.phone)) as phone
  21. FROM stuff as st
  22. LEFT JOIN stuff_phone ph1 ON st.id = ph1.id
  23. GROUP BY st.Id, st.name
  24.  
  25. SELECT st.Id, st.name, ph.phone FROM stuff_id as st
  26. INNER JOIN stuff_phone as ph ON ph.stuffid = st.Id
  27. WHERE ph.Id =
  28. (SELECT Top 1 id from stuff_phone
  29. WHERE st.Id = stuff_phone.stuffId
  30. Order By stuff_phone.preference)
  31. GROUP BY st.Id, st.name, ph.phone
Add Comment
Please, Sign In to add comment