select or count based on condition

  • [font="Courier New"]I have a query that returns results similar to this:

    Name-Order-Item - ItemShipDateTime

    Joe - O23 - I11 - 2013-07-29 13:05:00

    Jim - O24 - I11 - 2013-07-29 13:07:00

    Jim - O24 - I12 - 2013-07-29 13:07:00

    Sue - O25 - I21 - 2013-07-29 14:05:00

    Sue - O25 - I44 - 2013-07-29 15:55:00

    Actually, I'm not interested in the Item. I just want to count when an order is complete, and get counts per hour. The result set above represents three complete orders. Discarding "Item," my unique results would be:

    Name-Order-ItemShipDateTime

    Joe - O23 - 2013-07-29 13:05:00

    Jim - O24 - 2013-07-29 13:07:00

    Sue - O25 - 2013-07-29 14:05:00

    Sue - O25 - 2013-07-29 15:55:00

    So, I'm getting closer to three.

    How would I SELECT (or COUNT) only rows that contain the MAX Value in the column ItemShipDateTime field where NAME and ORDER are identical?

    [/font]

    Thanks!

  • Pretty sparse on the details but maybe something like this?

    select Name, Order, max(ItemShipDateTime)

    from SomeTable

    group by Name, Order

    If that isn't what you are looking for we will need a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean,

    That's exactly what I needed - thanks!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply