jaymc Posted April 23, 2008 Share Posted April 23, 2008 When using GROUP how can you get it to return the highest value in one of the affected rows for example, if there are 20 usernames in a table, for each row there is a time field of which are all different if we are doing GROUP BY username, how can I get it to pull out the highest time too At the moment I am unable to have any control of what time field it pulls out. I have tried using ORDER by and MAX() but it doesnt work Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 23, 2008 Share Posted April 23, 2008 MAX Should work: SELECT username,MAX(time) FROM tablename GROUP BY username Quote Link to comment Share on other sites More sharing options...
mwasif Posted April 23, 2008 Share Posted April 23, 2008 Look at http://jan.kneschke.de/projects/mysql/groupwise-max Quote Link to comment Share on other sites More sharing options...
jaymc Posted April 23, 2008 Author Share Posted April 23, 2008 MAX Should work: SELECT username,MAX(time) FROM tablename GROUP BY username That worked, but not when I put the rest in $querya = "SELECT count(views) as count, SUM(views) as viewers, gallery, timestamp, MAX(valid) FROM gallery WHERE gallery.user = 'peter' GROUP BY gallery"; Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 23, 2008 Share Posted April 23, 2008 What is the error message you are getting? 'timestamp' will give you problems, because you are grouping on gallery. Put a MAX() or something around that too. Quote Link to comment Share on other sites More sharing options...
jaymc Posted April 23, 2008 Author Share Posted April 23, 2008 There is no error message, it just displays what ever it wants I cant put a MAX around timestamp as that will conflict with the max around valid Quote Link to comment Share on other sites More sharing options...
fenway Posted April 23, 2008 Share Posted April 23, 2008 At the moment I am unable to have any control of what time field it pulls out. I have tried using ORDER by and MAX() but it doesnt work Can you give us an example? Quote Link to comment Share on other sites More sharing options...
jaymc Posted April 23, 2008 Author Share Posted April 23, 2008 Sure $q = "SELECT count(views) as count, SUM(views) as viewers, gallery, MAX(valid), timestamp FROM gallery WHERE user = 'jamie' GROUP BY gallery"; Or if you mean an example of data returned, well, its not returning the row in that GROUP which has the highest value for valid. Its as if it ignores that altogether Quote Link to comment Share on other sites More sharing options...
fenway Posted April 23, 2008 Share Posted April 23, 2008 I'm not sure what you mean... "the row"? It will get many rows... and timestamp is meaningless with aggregate functions. Quote Link to comment Share on other sites More sharing options...
jaymc Posted April 23, 2008 Author Share Posted April 23, 2008 Ok, its for a gallery system You click on a member and it will display there galleries. They may have 1 gallery they may have 20, it displays each of there gallerys. Of course if you click on one of the gallerys it then goes in and displays all the pictures but thats irrelivent The point is and my problem, I want a certain picture displayed for each of the galleries, the image is the one which has the highest value for valid if there where 10 images and I did a GROUP, it would select one of them images by default I want to choose which image is selects as default As well as doing that, as part of image name from the row returned, I also get the gallery name which is the object Quote Link to comment Share on other sites More sharing options...
fenway Posted April 24, 2008 Share Posted April 24, 2008 Ah.. .this sounds like the standard groupwise maximum issue... you can't do both without a join. Quote Link to comment Share on other sites More sharing options...
jaymc Posted April 24, 2008 Author Share Posted April 24, 2008 Dam, ok thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.