3

I'm trying to design a mysql query to retrieve the row for each city with the most recent trans_date and trans_count. I only want to return a single row per city.

Table transactions

------------------
id = integer
trans_date = date
trans_city = varchar
trans_count = integer

Sample Data

--------------
id       trans_date     trans_city     trans_count
--       ----------     ----------     -----------
1        2011-01-10     seattle        2104
2        2011-04-15     seattle        2072
3        2011-05-30     seattle        2057
4        2010-04-27     houston        5622
5        2010-04-30     houston        241
6        2010-05-25     houston        261

Desired Query Results (one row per city with the most recent date and count for that city)

---------------------
id       trans_date     trans_city     trans_count
--       ----------     ----------     -----------
3        2011-05-30     seattle        2057
6        2010-05-25     houston        261

None of the samples I have found return this result set I'm looking for. Any help appreciated.

Thanks,

-Scott

6 Answers 6

3

How about

SELECT * FROM 
  (
  SELECT id,trans_date,trans_city,trans_count 
  FROM transactions 
  ORDER BY trans_Date DESC) X 
GROUP BY trans_city 

I can't add comments on the other answers but you need the nesting because otherwise the group by applies before the order by and doesn't give you the expected answer.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 @Bob, creative solution to fix the random picking of rows by the group by clause that MySQL allows for.
1

Here's a version that works on SQL Server, just for posterity's sake since the upvoted solution doesn't.

SELECT
    t.*
FROM
    transactions AS t,
    (SELECT
        MAX(trans_date) AS max_date
    FROM
        transactions
    GROUP BY trans_city) AS subquery
WHERE
    subquery.max_date = t.trans_date

Comments

1
SELECT id, 
       trans_date, 
       trans_city, 
       trans_count 
FROM YourTable 
GROUP BY trans_city
ORDER BY trans_date DESC, trans_count;

1 Comment

The group by clause should come before the order by clause
0
with distinct_cities ( select max(id) as id, trans_city
from table
group by trans_city)

select t.id, t.trans_date, t.trans_city, t.trans_count
from table t join distinct_cities d on t.id = d.id

Comments

0
SELECT DISTINCT `trans-city` 
FROM `table` 
ORDER BY `trans_date` DESC, `trans_count` DESC;

Comments

0

start with an inner pre-query for the last date per city.. then join based on that most recent entry per city

select t2.*
   from
      ( select t1.trans_city,
               max( t1.trans_date ) as MostRecentDate
           from 
               Transactions t1
           group by
               t1.trans_city ) PreQuery

      join Transactions t2
         on PreQuery.Trans_City = t2.Trans_City
         and PreQuery.MostRecentDate = t2.Trans_Date

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.