10

For those that have been helping me along the way with this twitter-clone, thank you!! With your help, I've managed to get most things working and finally now to the last steps of the followers function.

Right now, I have a dataset with the following fields: username, tweet, date

An example of the data could look like:

Username    Tweet             Date
kenny       hi!               2011-10-07 19:07:00
stan        hello             2011-10-05 18:07:00
kenny       looks like rain   2011-10-05 17:07:00
stan        hello             2011-10-05 14:07:00
cartman     authoritay!       2010-10-05 14:07:00

And I've been wrestling with the SQL statement that would produce a data set in which each user appears only once with their latest tweet. So, based on the above, something that looks like this:

Username    Tweet             Date
kenny       hi!               2011-10-07 19:07:00
stan        hello             2011-10-05 18:07:00
cartman     authoritay!       2010-10-05 14:07:00

I've been googling sql searches and have tried variations of COUNT, DISTINCT, MAX, but to no avail. Any help would be greatly appreciated! Thank you!

4
  • Are you using MySql or MS SQL database? Oct 7, 2011 at 10:34
  • Try using distinct expression, what's the SQL script statement your using? Oct 7, 2011 at 10:35
  • Hi, I'm using a mysql database
    – kurisukun
    Oct 7, 2011 at 10:36
  • I tried a ton of statements over the course of the day, unfortunately. I tried DISTINCT again just now on the username, but it didn't produce any effect. I actually simplified my dataset for the purpose of posting this question, but my actual statement was something like this: SELECT DISTINCT username, content, date from tweets WHERE user_id IN (SELECT users.id FROM users INNER JOIN user_users ON users.id = user_users.followed_user_id WHERE user_users.user_id = 1) ORDER BY date desc
    – kurisukun
    Oct 7, 2011 at 10:39

3 Answers 3

9
select d1.username, d1.tweet, d1.date from data d1 where d1.date = 
    (select max(d2.date) from data d2 where d1.username = d2.username)
1
  • thank you! I don't understand this syntax, but let me study it.
    – kurisukun
    Oct 7, 2011 at 10:53
9

Would it not work just by

select distinct username, tweet, date order by date desc

(This is MSSQL syntax)

With new data in hand:

SELECT DISTINCT tweets.username, content, date from tweets 
WHERE user_id IN (
  SELECT users.id FROM users 
  INNER JOIN user_users ON users.id = user_users.followed_user_id 
WHERE user_users.user_id = 1) 
ORDER BY date desc
4
  • Actually, that was almost the exact statement I tried (I think). My actual dataset is a bit more complicated as I'm pulling the data from other tables. I tried this, but it didnt make each username appear only once: SELECT DISTINCT username, content, date from tweets WHERE user_id IN (SELECT users.id FROM users INNER JOIN user_users ON users.id = user_users.followed_user_id WHERE user_users.user_id = 1) ORDER BY date desc
    – kurisukun
    Oct 7, 2011 at 10:41
  • Or maybe not... What columns do you actually have in your tweets table? Oct 7, 2011 at 10:45
  • Hi, thanks, but actually, I just tried explicitly putting the tweets.username , but still no luck. :(
    – kurisukun
    Oct 7, 2011 at 10:46
  • Actually, I realized I'm not supposed to have a username within my tweets table. So now my tweets table looks like this: tweets (id, user_id, content) I just tried DISTINCT tweets.user_id, but the resulting set still included duplicate (or more) user_ids
    – kurisukun
    Oct 7, 2011 at 10:51
1
SELECT f.*
FROM (
      SELECT Username, MAX(`Date`) as maxval
      FROM  table GROUP BY Username    
     ) AS x INNER JOIN table AS f
ON f.Username  = x.Username  AND f.`Date`= x.maxval

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.