Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 21, 2012 05:15 AM by GmGregori
Dec 20, 2012 12:16 PM|LINK
How can I select two cols while one is in max.
select max(time) as time from albums_data where album_userid =27;
I was using this its going fine.
But If I want to get another col like source col it gives error : In aggregate and grouping expressions, the SELECT clause can contain only aggregates and grouping expressions. [ Select clause = ,source ]
select source, max(time) as time from albums_data where album_userid = 27
Dec 20, 2012 12:21 PM|LINK
Dec 20, 2012 12:28 PM|LINK
You can use Group by
select source, max(time) as time from albums_data where album_userid = 27 group by source
Dec 20, 2012 02:49 PM|LINK
oned_gk But this gives me all the fields. While I just want to get one result with the max time.
Dec 20, 2012 09:40 PM|LINK
As oned_gk and senthilwaits answered:
The above will show 2 columns, "source" and "time". No other columns should be displayed.
Dec 21, 2012 03:44 AM|LINK
If you want only one row you can use
select top 1 source, time from albums_data where album_userid = 27 order by time desc
Dec 21, 2012 05:15 AM|LINK
SELECT * WHERE album_userid = 27 AND time IN (SELECT MAX(time) FROM albums_data WHERE album_userid =27);
but senthilwaits's answer is better (I haven't seen it before replying).