Reputation: 13
I have a nested query. Any suggestion on how i can rewrite the nested query so that I am able to create a view with it
Here is the query
select *
from (select * from tracking_table order by id desc) x
group by labref);
Upvotes: 0
Views: 27
Reputation: 780939
There doesn't seem to be any need for the nested query. It can just be:
SELECT *
FROM tracking_table
GROUP BY labref
ORDER BY id DESC
But both queries probably don't do what you want. All the columns will be chosen from unpredictable rows in each group, and there's no guarantee that different columns will come from the same row. You're also not guaranteed to get the highest ID in each group.
If you want the row with the highest ID for each labref
, see SQL select only rows with max value on a column
Upvotes: 2