Guest User

Untitled

a guest
Feb 25th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. SELECT c.account_id,
  2. CASE
  3. WHEN c.last_sale IS NULL AND MAX(s.sale_date) IS NOT NULL THEN MAX(s.sale_date)
  4. WHEN MAX(s.sale_date) IS NULL THEN c.last_sale
  5. WHEN MAX(s.sale_date) IS NOT NULL AND c.last_sale IS NOT NULL AND c.last_sale < s.sale_date THEN s.sale_date
  6. ELSE c.last_sale
  7. END AS 'sale_date'
  8. FROM customers c
  9. LEFT JOIN sales s ON c.account_id = s.account_id
  10. WHERE sale_date <> ''
  11. GROUP BY c.account_id
Add Comment
Please, Sign In to add comment