Max Time of Max day

  • Hello,

    I have a table called Transaction

    Date ——- Date Time ——— Status

    1/1/18 6:55 400

    1/12/18 6:20 500

    1/12/18 6:35 400

    1/12/18 6:40 400

    I’m trying to get

    1/1/18 6:55 400

    1/12/18 6:40 400

    Becauae both rows have status = 400 and they are the max time of each date.

    How do I write a sql to accomplish that?

    Thanks in advance.

  • irehman - Wednesday, October 31, 2018 4:56 PM

    Hello,I have a table called TransactionDate ——- Date Time ——— Status1/1/18 6:55 4001/12/18 6:20 5001/12/18 6:35 4001/12/18 6:40 400I’m trying to get 1/1/18 6:55 4001/12/18 6:40 400Becauae both rows have status = 400 and they are the max time of each date.How do I write a sql to accomplish that?Thanks in advance.

    Something like the following should do the trick...

    WITH
        cte_AddRN AS (
            SELECT
                t.[Date],
                t.[Time],
                t.[Status],
                rn = ROW_NUMBER() OVER (PARTITION BY t.[Date] ORDER BY t.[Time] DESC)
            FROM
                dbo.Transactions t
            )
    SELECT
        *
    FROM
        cte_AddRN ar
    WHERE
        ar.rn = 1;

  • Jason A. Long - Wednesday, October 31, 2018 5:30 PM

    irehman - Wednesday, October 31, 2018 4:56 PM

    Hello,I have a table called TransactionDate ——- Date Time ——— Status1/1/18 6:55 4001/12/18 6:20 5001/12/18 6:35 4001/12/18 6:40 400I’m trying to get 1/1/18 6:55 4001/12/18 6:40 400Becauae both rows have status = 400 and they are the max time of each date.How do I write a sql to accomplish that?Thanks in advance.

    Something like the following should do the trick...

    WITH
        cte_AddRN AS (
            SELECT
                t.[Date],
                t.[Time],
                t.[Status],
                rn = ROW_NUMBER() OVER (PARTITION BY t.[Date] ORDER BY t.[Time] DESC)
            FROM
                dbo.Transactions t
            )
    SELECT
        *
    FROM
        cte_AddRN ar
    WHERE
        ar.rn = 1;

    This would have been a whole lot easier if they hadn't split the date and time into two columns.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jason A. Long - Wednesday, October 31, 2018 5:30 PM

    irehman - Wednesday, October 31, 2018 4:56 PM

    Hello,I have a table called TransactionDate ——- Date Time ——— Status1/1/18 6:55 4001/12/18 6:20 5001/12/18 6:35 4001/12/18 6:40 400I’m trying to get 1/1/18 6:55 4001/12/18 6:40 400Becauae both rows have status = 400 and they are the max time of each date.How do I write a sql to accomplish that?Thanks in advance.

    Something like the following should do the trick...

    WITH
        cte_AddRN AS (
            SELECT
                t.[Date],
                t.[Time],
                t.[Status],
                rn = ROW_NUMBER() OVER (PARTITION BY t.[Date] ORDER BY t.[Time] DESC)
            FROM
                dbo.Transactions t
            )
    SELECT
        *
    FROM
        cte_AddRN ar
    WHERE
        ar.rn = 1;

    Here is a boring version - no CTE's, no row numbering:

    SELECT [Date], MAX([Time], Status
    from dbo.Transactions
    group by [Date], Status

    _____________
    Code for TallyGenerator

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

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