+ Reply to Thread
Results 1 to 7 of 7

Thread: Aggregates

  1. #1
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    243
    Rep Power
    4

    Aggregates

    Ok, I know I'm going to feel really silly when someone answers this because it should be very basic, but I'm at a loss at this point on how to select a record based on a min/max value. I realize I can use a Having clause, but it doesn't produce what I want.

    Take the following data:

    Create Table Customer (CustomerId int primary key, DateofBirth int);
    Insert Into Customer (CustomerId, DateOfBirth)
    Values (1, 19010801);
    Insert Into Customer (CustomerId, DateOfBirth)
    Values (2, 19100101);
    Insert Into Customer (CustomerId, DateOfBirth)
    Values (3, 19201201);

    Select * from Customer

    Results:
    CustomerId DateOfBirth
    1 19010801
    2 19100101
    3 19201201

    Ok, so if I want to know the customerid of the person with the min or max date of birth, how would I go about it?

    I can write:

    Select customerid, Min(dateofbirth), Max(dateofbirth)
    From Customer
    Group by customerid


    but it just yields all records grouped by customerid. What if I wanted to see just the customerid and the dateofbirth of the min or max?

    Thanks!

  2. #2
    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,382
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    You could try
    Code:
    Select customerid
    From Customer
    where DateOfBirth = (Select Max(DateOfBirth) from Customer)
    
    Note that it may return more than 1 records if you have customers born on the same date.

    [Edit]
    Or another way:
    Code:
    Select Top 1 customerid
    From Customer
    order by DateOfBirth Desc
    
    Last edited by Wolffy; August 31st, 2010 at 05:00 PM.
    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.

  3. #3
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    243
    Rep Power
    4

    See! I told you I'd feel stupid Thanks Wolffy..as usual

  4. #4
    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,382
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    No problem. Some of these 'get the latest record' queries can be quite interesting. Just curious as to why you are using an int for DateOfBirth rather than a DateTime. DateTime can be much more versatile.
    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.

  5. #5
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    243
    Rep Power
    4

    I'm not! The application is I think the database that we use was originally created in the mid 90's and they never made the leap to start using datetime fields. Took me about 6 months of racking my brains to get past all the problems with that. As we've discussed on numerous occasions, I can get onto SQL Server 2005, but most of the work I do needs to be done client-side on a limited query writing tool. One of the tools doesn't even recognize datetime unless it's converted to a character...so I have to do some goofy conversions thusly:

    EX:

    Declare @Date INT
    Set @Date=20100101

    SELECT CONVERT(CHAR(8), Convert(DATETIME, Convert(char(8), @Date,10)),10)

    Results:
    01-01-10

    If I Just say:

    Declare @Date INT
    Set @Date=20100101

    SELECT Convert(DATETIME, Convert(char(8), @Date,10))

    The column disappears and the log says that it doesn't recognize the datetime data type.

    My life is full of workarounds!

  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,382
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Well, so long as you have a thought about it and have an reason for making the exception, then power on, dude. Misuse of data types is my pet SQL peeve, so I feel obligated to call it out when I see it
    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 Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    243
    Rep Power
    4

    I agree! I actually had a chance to meet with the developer of the application about a year ago and picked his brain on all the weirdness in the application/db.

    Very interesting stuff...at the very least, I've had real life experience of things to avoid!

+ Reply to Thread

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