Coder Profile - Show off your skills, get a coder profile.
 
 
 
  
Posted: 1.5 Year Ago 

Yuripro84
United States
Contrib Level: 10
Total Posts: 1,132
So let's say I have this table:

TABLE NAME: purchases
ID.....Name...Amount.....Date
1......Ben........2................3/6/2010
2......Chris.....3................3/8/2010
3......John......4................3/10/2010
4......Chris.....3................3/12/2010

No practical use for such a table, but then let's say I run the following Query:
SELECT *, SUM(*)
FROM `purchases`
GROUP BY `name`;

The follow will output:
1...Ben.....2....3/6/2010
2...Chris..6....3/8/2010
3...John...4....3/10/2010

Well, I was wanting for it to group in reverse order. I want it to show Chris after John, and show record 4 Chris 6 3/12/2010. I want the latest purchase date. I'm actually pulling a LOT of data from a table in reverse order by their ID. I need their last purchase date, and it shows their FIRST purchase date. Is the only way to do this is to make the FROM value the purchases table queried in reverse ORDER BY? Like this?

SELECT *,SUM(*)
FROM (SELECT * FROM purchases as `a` ORDER BY `a`.`id` DESC)
GROUP BY `name`;

I don't know if the above will actually work, but I'm trying to avoid using a query inside of a query. If needed though, it's not used very often, but is still a lot of overhead I'd rather not get myself into. If anyone has any suggestions, please let me know, they'd be greatly appreciated (:

-Christopher Burkhouse
Posted: 1.5 Year Ago 

Yuripro84
United States
Contrib Level: 10
Total Posts: 1,132
Hey guys, I just realized it doesn't always grab the first or last record, seems to be random... not quite sure what to do. Should I find a different method?

Thanks,
Christopher Burkhouse
Page 1 of 1
 
 
Latest News About Coder Profile
Coder Profile Poll
If you made money from keeping your profile up to date, say $30 and up, per month. What extra time would you spend on your profile?

No extra time
A few hours at weekends
A whole day each week
Every minute i can get free


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
3.49 Years Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2012, Scott Thompson, All Rights Reserved