tags:

views:

46

answers:

2

Hi

This is probably something very simple, so forgive my blonde moment :)

I have a table 'album'

* albumId
* albumOwnerId (who created)
* albumCSD (create stamp date)

Now what I am trying to do is to select the top 10 most recently updated albums. But, I don't want 10 albums from the same person coming back - I only want one album per unique person. I.E 10 albums from 10 different people.

So, this is what I have below, but it is not working properly and I just can't figure out why. Any ideas?

Thanks

SELECT DISTINCT(albumOwnerId), albumId
FROM album
ORDER BY albumCSD DESC
LIMIT 0,10

Here is some example data, followed by what I am trying to get. Hope this makes it clearer.

DATA:

albumOwnerID, albumId, albumCSD
18, 194, '2010-10-23 11:02:30'
23, 193, '2010-10-22 11:39:59'
22, 192, '2010-10-12 21:48:16'
21, 181, '2010-10-12 20:34:11'
21, 178, '2010-10-12 20:20:16'
19, 168, '2010-10-12 18:31:55'
18, 167, '2010-10-11 21:06:55'
20, 166, '2010-10-11 21:01:47'
18, 165, '2010-10-11 21:00:32'
20, 164, '2010-10-11 20:50:06'
17, 145, '2010-10-10 18:54:24'
17, 144, '2010-10-10 18:49:28'
17, 143, '2010-10-10 18:48:08'
17, 142, '2010-10-10 18:46:54'
16, 130, '2010-10-10 16:17:57'
16, 129, '2010-10-10 16:17:26'
16, 128, '2010-10-10 16:07:21'
15, 119, '2010-10-10 15:24:28'
15, 118, '2010-10-10 15:24:11'
14, 100, '2010-10-09 18:22:49'
14, 99, '2010-10-09 18:18:46'
11, 98, '2010-10-09 15:50:13'
11, 97, '2010-10-09 15:44:09'
11, 96, '2010-10-09 15:42:28'
11, 95, '2010-10-09 15:37:25'

DESIRED DATA:

18, 194, '2010-10-23 11:02:30'
23, 193, '2010-10-22 11:39:59'
22, 192, '2010-10-12 21:48:16'
21, 181, '2010-10-12 20:34:11'
19, 168, '2010-10-12 18:31:55'
17, 145, '2010-10-10 18:54:24'
16, 130, '2010-10-10 16:17:57'
15, 119, '2010-10-10 15:24:28'
14, 100, '2010-10-09 18:22:49'
11, 98, '2010-10-09 15:50:13'
+1  A: 
select albumOwnerID, albumID
from album
Group by albumOwnerID, albumID
Order by albumcsd desc
LIMIT 0,10

EDIT:

select albumOwnerID, albumID 
from album
where albumOwnerID in (select distinct albumOwnerID from album order by albumCSD )
LIMIT 0,10
Sachin Shanbhag
Hi Sachin. That did not work. It yields exactly the same result with or without the group by clause. AlbumOwnerId is not distinct.
Cheeky
@Cheeky - Can albumID be different for same ownerID? If yes, then please use `Group by albumOwnerID` and remove albumID from group by
Sachin Shanbhag
That is what I tried originally but is incorrect, as the albumOwnerIds come back in the wrong order.
Cheeky
and if you try to select albumOwnerID, albumID from (select albumOwnerId, albumId FROM album ORDER BY albumCSD DESC) Group by albumOwnerID LIMIT 0,10
Mur Votema
Hi Mur - that query does not even execute. Error - "Every derived table must have its own alias"
Cheeky
@cheeky - Have edited my query, just check if this works.
Sachin Shanbhag
Hi Sachin - thanks, but that did not work at all. Results were far off. Maybe I will post some example data at the top to illustrate more clearly what I'm expecting.
Cheeky
@Cheeky - Sorry for trouble, but I do not have mysql installed. Hence I could only give you query ideas. I guess Mur's query is nearest. You can resolve the error you are getting by adding some alias after select subquery like `(select .... ) x` where x is alias.
Sachin Shanbhag
Thanks for the help anyway Sachin.
Cheeky
+1  A: 

I get results, you want to have, with this query

SELECT albumOwnerID, albumId, albumCSD
FROM album
WHERE albumCSD in
(SELECT  Max(album.albumCSD) AS MaxvonalbumCSD
FROM album
GROUP BY album.albumOwnerID);

However in MS Access

Mur Votema
Hi Mur - Thanks - that works just fine in MySql and provided the required result! Just need to be ordered DESC, but that's nothing. Thanks a lot!
Cheeky