ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
My question:
when dailies_shot_name is the same, how can I return only the most recent (determined by the dailies_ctime field) entry?
In other words, if there were no LIMIT on the above query, I would get about 1500 entries, but there are only about 150 distinct shot_name entries. How can I return only the most recent, distinct shot name while also returning the other fields?
Aren't joins for linking multiple tables together? All of this info is in one table. Can you join one table with itself? If so, will that help with what I'm after? I'm not seeing it in the docs.
Have you tried a 'group by' dailies_shot_name and 'having max(dailies_ctime)' in your query? This is from memory, but I think that should do. But the basic idea is to group the results, then get the largest item in the group.
SELECT dailies_shot_name,dailies_id,MAX(dailies_ctime)
FROM dailies
WHERE dailies_project_id = '%d'
GROUP BY dailies_shot_name;
while it was giving back the correct shot_name and ctime, it was giving the incorrect id... the same id that would be returned if MAX(dailies_ctime) was not used.
So I've come up with a solution, but it's pretty inefficient. It takes 4 seconds to calculate on a table with about 1700 columns, returning about 150 columns. If anyone knows how to optimize, I'd love to see it. Here's what works:
Code:
SELECT dailies_id,dailies_shot_name,dailies_department,dailies_ctime
FROM dailies d1
WHERE d1.dailies_project_id='1110'
AND d1.dailies_ctime = (SELECT MAX(d2.dailies_ctime) FROM dailies d2 WHERE d1.dailies_shot_name = d2.dailies_shot_name);
Per the docs, I also tried this:
Code:
SELECT d1.dailies_id,d1.dailies_shot_name,d1.dailies_department,d1.dailies_ctime
FROM dailies d1
LEFT JOIN dailies d2
ON d1.dailies_shot_name = d2.dailies_shot_name
AND d1.dailies_ctime < d2.dailies_ctime
WHERE d2.dailies_ctime IS NULL
AND d1.dailies_project_id='1110';
Just for anyone else reading this, I think you mean 'rows', not 'columns' (I hope)
But for that kind of requirement, nested sub-select is normal. 4 secs isn't too bad. Maybe you need better indexes if you've got very large tables?
I've actually found that sometimes its quicker overall to wrap 2 separate simple selects in eg Perl and retrieve the wanted rows from the Perl data structures.
Solved. Thanks to the docs and a little finagling, this does the same as above, but in 0.06 seconds rather than ~4 seconds.
Code:
SELECT d1.dailies_id,d1.dailies_shot_name,d1.dailies_department,d1.dailies_ctime
FROM dailies d1, (SELECT dailies_shot_name, MAX(dailies_ctime) AS dailies_ctime FROM dailies GROUP BY dailies_shot_name) as d2
WHERE d1.dailies_project_id ='1110'
AND d1.dailies_shot_name = d2.dailies_shot_name
AND d1.dailies_ctime = d2.dailies_ctime;
The magic being the subquery that QUICKLY produces a very nice table that can easily be used for comparison.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.