Guest User

Untitled

a guest
Aug 29th, 2013
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. SET SESSION group_concat_max_len = 99;
  2. select o.customer_id, substring_index(m.orders,',', 1) as order1,
  3. (case when numc >=2 then substring_index(substring_index(m.orders, ',', 2), ',', -1)end) as order2,
  4. (case when numc >=3 then substring_index(substring_index(m.orders, ',', 3), ',', -1)end) as order3,
  5. (case when numc >=4 then substring_index(substring_index(m.orders, ',', 4), ',', -1)end) as order4,
  6. (case when numc >=5 then substring_index(substring_index(m.orders, ',', 5), ',', -1)end) as order5
  7. from orders o,
  8. (select group_concat(date order by date desc) as orders, count(*) as numc
  9. FROM orders) m
  10. where country_id='27'
  11. group by customer_id
  12.  
  13. select o.customer_id, substring_index(group_concat(date order by date desc),',', 1) as order1,
  14. (case when count(*) >=2 then substring_index(substring_index(group_concat(date order by date desc), ',', 2), ',', -1)end) as order2,
  15. (case when count(*) >=3 then substring_index(substring_index(group_concat(date order by date desc), ',', -1)end) as order3,
  16. (case when count(*) >=4 then substring_index(substring_index(group_concat(date order by date desc), ',', 4), ',', -1)end) as order4,
  17. (case when count(*) >=5 then substring_index(substring_index(group_concat(date order by date desc), ',', 5), ',', -1)end) as order5
  18. from orders o
  19. where country_id='27'
  20. group by customer_id
Advertisement
Add Comment
Please, Sign In to add comment