+ Reply to Thread
Results 1 to 10 of 10

Thread: Select only first instances of a given criteria...

  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

    Select only first instances of a given criteria...

    I seem to be the king of MySQL questions, given the lack of board activity...

    Here's one for ya. I have a 'changelog' table in my database that tracks changes to the main entries. Any time an entry is updated, everything about it is copied to the changelog table and given a unique id. My question is as follows: how can I pull only the first instance of a given record in a query?

    Here's an example. Say I have a dozen rows in this table, each having their own changelog_id. There is another field named 'id' that reflects the record's actual record in the main table. Now, out of this dozen, six have individual id numbers, two have the same id and the remaining four have the same id. So if I were to run the query:
    Code:
    SELECT count(*) as idx,id FROM changelog GROUP BY id ORDER BY idx ASC;
    
    I'd end up with:
    Code:
    count	id
    1	1
    1	2
    1	3
    1	4
    1	5
    1	6
    2	7
    4	8
    
    Which is well and good. Now to the part where I make this difficult.

    There is another field in the table named 'input_date' which simply shows when the data was first input in the database. The problem comes in that each time the data is updated and the subsequent 'changelog' copy is written, this date is also copied. So if I were to look for all data with a given input_date, I would get every instance of it in the changelog. What I'm trying to do is find just the FIRST instance of it so I can see who it was entered by.

    I thought I might be able to do something with DISTINCT, but that doesn't seem to work, at least not with the data I'm trying to read. I then though I could use MIN(), but that doesn't seem to work, either.

    I would also like to mention there is another field named lastedit which is a datetime field. If there's some way to pull all records with the input_date of X and the smallest value for lastedit, I'd love to hear it.
    Last edited by bryceowen; July 7th, 2009 at 03:38 PM.

  2. #2
    Super Sarcasm Mistress mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere's Avatar
    Join Date
    Mar 2008
    Location
    Wide Awake In Dreamland
    Posts
    830
    Rep Power
    8

    not sure i'm understanding it all, but can't you just do ...
    Code:
    SELECT id, input_date, MIN(lastedit) 
    FROM changelog 
    GROUP BY id, input_date 
    ORDER BY idx ASC;
    
    Quote of the Month:
    INSIGHT: When the going gets tough, the tough get going. The smart left a long time ago.

    Questions to Ponder:
    Are people more violently opposed to fur rather than leather because it's much easier to harass rich women than motorcycle gangs?

    iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
    copyright © 2008 sbenj69

    Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.

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

    Sadly, no. The reason being that it selects only one record; that with the smallest datetime for the lastedit.

  4. #4
    Super Sarcasm Mistress mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere's Avatar
    Join Date
    Mar 2008
    Location
    Wide Awake In Dreamland
    Posts
    830
    Rep Power
    8

    didn't you say you only wanted the first record for an ID?
    What I'm trying to do is find just the FIRST instance of it so I can see who it was entered by.
    okay ... i'll admit it, i'm confused by your question.
    Quote of the Month:
    INSIGHT: When the going gets tough, the tough get going. The smart left a long time ago.

    Questions to Ponder:
    Are people more violently opposed to fur rather than leather because it's much easier to harass rich women than motorcycle gangs?

    iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
    copyright © 2008 sbenj69

    Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.

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

    O.K... Let's take a step back.

    Say I have the following records:
    Code:
    id	old_id	input_date	lastedit
    1	10	2009-07-07	2009-07-07 10:00:00
    2	11	2009-07-07	2009-07-07 10:10:00
    3	11	2009-07-07	2009-07-07 11:37:00
    
    Now I want to pull the data with an input date of 2009-07-07 and the earliest lastedit time, the result in this case being the first two rows. If I use MIN(lastedit), the only row I get is the first one.

  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

    You are probably going to need to use a subquery along the lines of this
    Code:
    Select A.id, A.old_id, A.input_date, A.lastedit
    From daTable A
    Join (
       Select input_date, min(lastedit) as lastedit
       From daTable 
       Group By input_date) as B
    On A.input_date = B.input_date and A.lastedit = B.lastedit
    
    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.

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

    I just tried
    Code:
    SELECT a.id, a.input_date, a.lastedit
    FROM changelog a
    JOIN (
    
    SELECT input_date, min( lastedit ) AS lastedit
    FROM changelog
    GROUP BY input_date
    ) AS b ON a.input_date = b.input_date
    AND a.lastedit = b.lastedit
    WHERE a.input_date = '2009-07-07'
    
    and I get the same result. (That is, I get only one record, the one with the earliest time for the lastedit.)

  8. #8
    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

    Ok fudge, I misread what you were looking for. Lemme do a re-think
    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.

  9. #9
    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

    OK, let's try it this way and see if I understand now:
    Code:
    Select a.id, a.old_id, a.input_date, a.lastedit
       From dTable A
       Join  (Select old_id, input_date, min(lastedit)
               From daTable
               Group By input_date, old_id) as B
         on    A.input_date = B.input_date
          And A.old_id = B.old_id
          And A.lastedit = B.lastEdit
    
    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.

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

    Preliminary testing shows that what you suggested is working, Wolffy. I'll let you know after I have a chance to review the returned data against the raw data, but I have a good feeling about it. You may have saved me from creating a whole new set of rules that govern the input of data.

+ Reply to Thread

Similar Threads

  1. Open Generic Report with Various Criteria (simple sample)
    By boblarson in forum Access Database Samples
    Replies: 1
    Last Post: March 5th, 2010, 09:55 AM
  2. save report with criteria as pdf file in A07
    By tuxalot in forum Microsoft Access
    Replies: 15
    Last Post: March 23rd, 2009, 09:23 PM
  3. select problem
    By todd2006 in forum ASP Development
    Replies: 7
    Last Post: February 12th, 2009, 07:53 PM
  4. Data Type Mismatch in Criteria Expression
    By alansidman in forum Microsoft Access
    Replies: 1
    Last Post: April 9th, 2008, 05:33 PM
  5. sQL select add '%' next to value
    By peebman2000 in forum SQL Development
    Replies: 8
    Last Post: March 28th, 2008, 11:21 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