Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 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 st.Id, st.name, coalesce(max(ph1.phone),max(ph2.phone)) as phone
  7. FROM stuff as st
  8. LEFT JOIN stuff_phone ph1 ON st.id = ph1.id and ph1.preference='p'
  9. LEFT JOIN stuff_phone ph2 ON st.id = ph2.id and ph2.preference!='p'
  10. GROUP BY st.Id, st.name
  11.  
  12. SELECT st.Id, st.name,
  13. coalesce(max(case when ph1.preference='p' then ph1.phone end),
  14. max(ph1.phone)) as phone
  15. FROM stuff as st
  16. LEFT JOIN stuff_phone ph1 ON st.id = ph1.id
  17. GROUP BY st.Id, st.name
  18.  
  19. SELECT st.Id, st.name, ph.phone FROM stuff_id as st
  20. INNER JOIN stuff_phone as ph ON ph.stuffid = st.Id
  21. WHERE ph.Id =
  22. (SELECT Top 1 id from stuff_phone
  23. WHERE st.Id = stuff_phone.stuffId
  24. Order By stuff_phone.preference)
  25. GROUP BY st.Id, st.name, ph.phone
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement