+ Reply to Thread
Results 1 to 6 of 6

Thread: Pulling total records and most recent data?

  1. #1
    Barn Regular bryceowen is on a distinguished road bryceowen's Avatar
    Join Date
    Sep 2008
    Location
    Jacksonville, FL
    Posts
    93
    Rep Power
    4

    Pulling total records and most recent data?

    I'm trying to accomplish this with one SQL statement, but I guess I'm a little out of practice, as I'm coming up with nothing.

    Let's say I have two entries in a database:
    Code:
    id	record	date			data1		data2
    1	1234	2009-12-10 00:00:01	abc		def
    2	1234	2009-12-10 02:00:00	def		ghi
    
    Now, I want to pull the total records where id=1234 and I also want to pull the data from the most recent entry. I try:
    Code:
    SELECT *,COUNT(*) AS TOTAL FROM history WHERE record='1234' ORDER BY date DESC LIMIT 1
    
    and it returns:
    Code:
    +--------+----------+---------------------+-------+-------+-------+
    | log_id | record   | date                | data1 | data2 | total |
    +--------+----------+---------------------+-------+-------+-------+
    |      2 | 1234     | 2009-12-10 02:00:00 | abc   | def   |     2 |
    +--------+----------+---------------------+-------+-------+-------+
    1 row in set (0.00 sec)
    
    It does this with ASC order, too. If I try it with MAX(date), I get the right date, but the data fields are not correct for the date.

    --EDIT--
    I found using a sub query will work, but is there a more efficient way?
    Code:
    SELECT *,(SELECT COUNT(*) FROM history WHERE record='1234') AS total FROM history WHERE record='1234' ORDER BY date DESC LIMIT 1
    
    Last edited by bryceowen; December 11th, 2009 at 01:59 PM. Reason: Found a possible solution...

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    I would probably do something like:
    Code:
    SELECT COUNT(log_id) FROM history WHERE record = '1234' AND date = (SELECT MAX(date) FROM history WHERE record = '1234')
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    The last query you have there is about the easiest way to do this. I've written queries like this just this way.

    And how efficient does it have to be with a 2 record table?
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  4. #4
    Barn Regular bryceowen is on a distinguished road bryceowen's Avatar
    Join Date
    Sep 2008
    Location
    Jacksonville, FL
    Posts
    93
    Rep Power
    4

    Well, a two-record table wouldn't require anything terribly efficient, but a, say 2,000,000 record table... Well, things start to slow down with sub-queries.

  5. #5
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    You should remove the asterick and put log_id or 1 in the aggregate function for improved performance:
    Code:
    SELECT *,(SELECT COUNT(1) FROM history WHERE record='1234') AS total FROM history WHERE record='1234' ORDER BY date DESC LIMIT 1
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  6. #6
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Quote Originally Posted by jmurrayhead View Post
    You should remove the asterick and put log_id or 1 in the aggregate function for improved performance:
    Code:
    SELECT *,(SELECT COUNT(1) FROM history WHERE record='1234') AS total FROM history WHERE record='1234' ORDER BY date DESC LIMIT 1
    
    That and make sure the record field has and index and that the index is being used.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

+ Reply to Thread

Similar Threads

  1. JOIN on most recent entry
    By richyrich in forum MySQL
    Replies: 7
    Last Post: November 11th, 2009, 01:56 PM
  2. Sub/Child Records
    By Flam in forum SQL Development
    Replies: 8
    Last Post: June 16th, 2009, 02:19 PM
  3. Replies: 8
    Last Post: March 20th, 2009, 10:03 AM
  4. Pulling from multiple tables...
    By bryceowen in forum SQL Development
    Replies: 2
    Last Post: September 15th, 2008, 08:57 PM
  5. Replies: 5
    Last Post: June 25th, 2008, 09:07 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

SEO by vBSEO