php - ORDER BY Not working with GROUP BY -


i have written sql code using group , order in here order not working can 1 me

select " . db_prefix . "leaderboard_scores.* , sum(" . db_prefix . "leaderboard_scores.score) total_score, " . db_prefix . "customer.firstname,          " . db_prefix . "customer.lastname " . db_prefix . "leaderboard_scores  join " . db_prefix . "customer on  " . db_prefix . "leaderboard_scores.philips_store_id = " . db_prefix . "customer.customer_id group " . db_prefix . "leaderboard_scores.phi_store_id order " . db_prefix . "leaderboard_scores.week desc 

this query work without php errors given middle rows without giving top weeks. weeks store using times stamp

actual query

select rc_leaderboard_scores.* , sum(rc_leaderboard_scores.score)  total_score, rc_customer.firstname, rc_customer.lastname  rc_leaderboard_scores  join rc_customer  on rc_leaderboard_scores.phi_store_id = rc_customer.customer_id  group rc_leaderboard_scores.phi_store_id  order rc_leaderboard_scores.week desc  

if need return last record every customer, need join subquery returns max(week) every customer id:

select   rc_leaderboard_scores.*,   m.total_score,   rc_customer.firstname,   rc_customer.lastname    (select   phi_store_id,             max(`week`) max_week,             sum(score) total_score,        rc_leaderboard_scores    group phi_store_id) m   inner join   rc_leaderboard_scores   on rc_leaderboard_scores.phi_store_id = m.phi_store_id      , rc_leaderboard_scores.`week` = m.max_week   inner join   rc_customer on m.phi_store_id = rc_customer.customer_id 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -