0

I have tried even simple SQL query too but I am not getting latest result in group by. I have tried these.

1)-

SELECT price_factors.* 
FROM (SELECT * FROM price_factors ORDER BY id DESC) price_factors 
GROUP BY price_factors.trait_id`

2)-

SELECT * 
FROM price_factors 
GROUP BY trait_id 
ORDER BY id DESC
Shadow
  • 33,525
  • 10
  • 51
  • 64
Abo Baker
  • 107
  • 2
  • 5
  • Hi! it would be helpful if you added the structure of your database and your desired output – Gamopo Apr 20 '20 at 17:09
  • 2
    Why are we using the `GROUP BY` clause? What purpose are we expecting that to serve? The specification is unclear. The specification would be better explained and illustrated by including some *sample* data and *expected* output. It's great that we've included SQL that doesn't return the expected result, but it falls short of explaining what the expected result is. (I think we need to first answer the question, how would we go about identifying which rows are the "latest records" ?) – spencer7593 Apr 20 '20 at 17:10
  • I got the answer guys thanks. Here is the answer select distinct price_factors.*, MAX(price_factors.id) as priceid, `cr`.`rate` from `price_factors` left join `currency_rates` as `cr` on `cr`.`from` = `price_factors`.`price_currency` and `cr`.`to` = 'EUR' order by `price_factors`.`id` desc – Abo Baker Apr 20 '20 at 17:48

0 Answers0