Results 1 to 7 of 7

Thread: MySQL Query Mystery

  1. #1
    Junior Registered
    Join Date
    Feb 2004
    Location
    Washington State
    Posts
    4

    MySQL Query Mystery

    I need to do a MySQL query to find the most recent instance (date_field) for each (symbol_field) in a given (group_field). Additionally there is an (id_field). All fields are in one table. Any help would be appreciated.

    An example group of records would be.........


    ____date __symbol____id____group

    2004-12-18__XYZ __ 00222__ Building
    2004-12-17__UVW __00224__ Building
    2004-12-16__RST__ 00231__Building
    2004-11-18__XYZ__00225__Building
    2004-11-17__UVW__00219__Building
    2004-11-16 __RST__00215__Building
    2002-09-18__ ABC__ 00025__Building

    The query would return...

    2004-12-18__XYZ__00222__Building
    2004-12-17__UVW__00224__Building
    2004-12-16__RST__ 00231__Building
    2002-09-18__ABC__00025__Building

    Every time. Thanks.

  2. #2
    Registered
    Join Date
    Sep 2004
    Posts
    51
    In SQL Server, the following query would give you what you need (if I understand your question):

    Code:
    SELECT *
     FROM table a
     INNER JOIN (SELECT date = MAX(date),
                                 symbol,
                                  group
                FROM table
                GROUP BY symbol, group
                ) b ON (a.date    = b.date    AND
                        a.symbol = b.symbol  AND
                        a.group   = b.group
                        )
    In MySQL, I *think* the query can be rewritten like this:

    Code:
    SELECT *
     FROM table a
     INNER JOIN (SELECT date = MAX(date),
                                 symbol,
                                 group
                  FROM table
                  GROUP BY symbol, group
                ) as b ON (a.date    = b.date    AND
                           a.symbol = b.symbol  AND
                           a.group   = b.group
                           )

    The only thing that's different in the second version is the "as b" part when aliasing the derived table.

  3. #3
    Senior Member AndyH's Avatar
    Join Date
    May 2004
    Location
    Australia
    Posts
    553
    Is it just me or would this be what you need:
    Code:
    SELECT *
    FROM `table`
    ORDER BY `date` DESC;
    I'm sure i'm missing something that makes it difficult...
    New website released. ya rly!

  4. #4

  5. #5
    Senior Member AndyH's Avatar
    Join Date
    May 2004
    Location
    Australia
    Posts
    553
    Code:
    SELECT *
    FROM `table`
    GROUP BY `symbol_field`, `group_field`
    ORDER BY `date` DESC;
    ???
    New website released. ya rly!

  6. #6
    Registered
    Join Date
    Sep 2004
    Posts
    51
    I think that for each symbol, he only wants the one associated with the last date. For example, if we reorder his source records and get rid of the ones that aren't the most recent (rows in blue), we end up with his results:

    2004-12-18__XYZ __ 00222__ Building
    2004-11-18__XYZ__00225__Building
    2004-12-17__UVW __00224__ Building
    2004-11-17__UVW__00219__Building
    2004-12-16__RST__ 00231__Building
    2004-11-16 __RST__00215__Building
    2002-09-18__ ABC__ 00025__Building

    So in my query above, the "derived table" subquery like this...


    Code:
    SELECT date = MAX(date),
                                 symbol,
                                  group
                FROM table
                GROUP BY symbol, group
    Would return

    2004-12-18__XYZ __ Building
    2004-12-17__UVW__ Building
    2004-12-16__RST__Building
    2002-09-18__ABC__Building

    That part is then joined back to the original table to get full rows of data.

    Note that this approach wouldn't work if a symbol had multiple records on the same date. For example, if the source data had 2 records for "XYZ" on 2004-12-18, then the final result set would have dups (nevertheless, they could be removed if the date included a time component).

  7. #7
    Junior Registered
    Join Date
    Feb 2004
    Location
    Washington State
    Posts
    4
    Sorry, I have been gone and won't be able to try things until the weekend. Below is what I use now. It works to get a set of all symbols for a group but doesn't always get the latest records for a symbol in a group. Confusing? It is to me too. Building queries is still Greek to me. Thanks.

    SELECT symbol_f,company,pr,rec,group,MAX(l_date), DATE_FORMAT(ld,'%m/%d/%y'),lsne FROM table1 WHERE lsne = 1 or lsne = 8 GROUP BY symbol_F HAVING grp='$var_g' AND MAX(l_date) > '2003-01-00'

Similar Threads

  1. Replies: 2
    Last Post: 02-20-2005, 11:30 PM
  2. Replies: 0
    Last Post: 01-30-2005, 08:33 PM
  3. Replies: 0
    Last Post: 11-25-2004, 01:06 AM
  4. Replies: 8
    Last Post: 11-12-2004, 04:10 PM
  5. Excluding rows in a MySQL Query
    By nlvp in forum Website Programming & Databases
    Replies: 4
    Last Post: 06-15-2004, 08:41 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •