/    Sign up×
Community /Pin to ProfileBookmark

nested query; query within a query

Data

id case_id date_notated
1 25 06/01/2010
2 25 06/21/2011
3 25 09/03/2012
4 25 05/01/2013
5 26 05/02/2013
6 26 05/04/2013
7 26 05/05/2013
8 26 05/07/2013

Desired Result

id case_id last_post
4 25 05/01/2013
8 26 05/07/2013

how do i write a query to get the result?

thanks!

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 29.2013 — If I'm guessing correctly as to what the criteria are:
<i>
</i>SELECT id, case_id, last_post
FROM table_name AS t1
WHERE last_post = (
SELECT MAX(last_post)
FROM table_name t2
WHERE t1.case_id = t2.case_id
)
ORDER BY case_id -- assuming you want this ordering, otherwise change this

This assumes that last_post is some sort of DATE, DATETIME, or TIMESTAMP column.

You'll want to make sure you have indexes on the case_id and last_post columns for best performance.
×

Success!

Help @inosent spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...