max not giving the latest one

  • CREATE TABLE test

    (

    createdon datetime,

    kms_quoteorder varchar(15)

    )

    INSERT INTO test VALUES ('2015-06-10 14:03:04.000','NULL')

    INSERT INTO test VALUES ('2015-07-22 15:13:39.000','15-37666')

    INSERT INTO test VALUES ('2015-07-22 16:13:39.000','15-37779/Rev7')

    INSERT INTO test VALUES ('2015-07-22 19:54:56.000','15-37779/Rev8')

    INSERT INTO test VALUES ('2015-07-22 20:26:41.000','15-35553/Rev9')

    INSERT INTO test VALUES ('2015-07-22 21:32:31.000','15-35553/Rev10')

    select * from test

    drop table test

    Here is more test data

  • Based on the above sample data, what is the expected results?

  • Also, is the string 'Null' a real Null or a string?

  • Overcomplicated this thread has become. Weak was the force.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • The NULL is a real NULL

    and I'm expecting the max kms_quoteorder

    so if there are no /REV then I should only see the number.

    The /REV stands for Revision. Poorly designed, I know. So we had a order number Revised 10 times at one point. They are wanting the see the latest

  • lcarrethers (8/10/2015)


    The NULL is a real NULL

    and I'm expecting the max kms_quoteorder

    so if there are no /REV then I should only see the number.

    The /REV stands for Revision. Poorly designed, I know. So we had a order number Revised 10 times at one point. They are wanting the see the latest

    What should it return for this row? INSERT INTO test VALUES ('2015-06-10 14:03:04.000','NULL')

    Don't give me a generic answer. Type out the actual values.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • I don't think they want to see any NULL values.

  • lcarrethers (8/10/2015)


    I don't think they want to see any NULL values.

    FYI, the NULL in this INSERT is NOT a null. It is a string whose value is the string (word) NULL.

    INSERT INTO test VALUES ('2015-06-10 14:03:04.000','NULL')

  • They want to ignore these NULLS, blank values in that column. so if the word null is typed in or blank, they don't want to see them

  • What's the maximum number of digits that the number after REV can be? Will it always be an integer?



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • 99 and it will always be a integer

  • This is a very messy query to right. If only the Rev # had a leading zero when necessary, then this would be simple.

    Would also be much easier, if the Rev # was stored as an integer in a separate column.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • If this doesn't give a full solution, it might give an idea.

    with base as (

    select

    *,

    rn = row_number() over (partition by left(kms_quoteorder,charindex('/',kms_quoteorder + '/') - 1)

    order by CAST( CASE WHEN kms_quoteorder NOT LIKE '%/Rev%' THEN 0

    WHEN RIGHT( kms_quoteorder, 2) LIKE 'v[0-9]' THEN RIGHT( kms_quoteorder, 1)

    ELSE RIGHT( kms_quoteorder, 2) END AS int) desc)

    from

    test

    )

    select createdon, kms_quoteorder

    from base

    where rn = 1;

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Take a look at this. It may not be exactly what you need, but it's a start.

    CREATE TABLE #test

    (

    createdon datetime,

    kms_quoteorder varchar(15)

    )

    INSERT INTO #test VALUES ('2015-06-10 14:03:04.000',NULL)

    INSERT INTO #test VALUES ('2015-07-22 15:13:39.000','15-37666')

    INSERT INTO #test VALUES ('2015-07-22 16:13:39.000','15-37779/Rev7')

    INSERT INTO #test VALUES ('2015-07-22 19:54:56.000','15-37779/Rev8')

    INSERT INTO #test VALUES ('2015-07-22 20:26:41.000','15-35553/Rev9')

    INSERT INTO #test VALUES ('2015-07-22 21:32:31.000','15-35553/Rev10')

    SELECT

    [QuoteOrder] AS [kms_quoteorder]

    ,[MAX_kms_quoteorder] = CASE WHEN RevNo != 0 THEN [QuoteOrder] + '/Rev' + CAST(RevNo AS VARCHAR(2))

    ELSE [QuoteOrder] END

    FROM

    (SELECT

    [QuoteOrder] = CASE WHEN CHARINDEX('/Rev', kms_quoteorder) > 0

    THEN LEFT(kms_quoteorder, CHARINDEX('/Rev', kms_quoteorder)-1)

    ELSE kms_quoteorder END

    ,[RevNo] = MAX(CASE WHEN CHARINDEX('/Rev', kms_quoteorder) > 0

    THEN CAST(RIGHT(kms_quoteorder, LEN(kms_quoteorder) - (CHARINDEX('/Rev', kms_quoteorder)+3)) AS INT)

    ELSE 0 END)

    FROM #TEST

    WHERE kms_quoteorder IS NOT NULL

    GROUP BY (CASE WHEN CHARINDEX('/Rev', kms_quoteorder) > 0

    THEN LEFT(kms_quoteorder, CHARINDEX('/Rev', kms_quoteorder)-1)

    ELSE kms_quoteorder END)

    ) [A]

    ORDER BY [kms_quoteorder]

    DROP TABLE #Test



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Thanks, this helps

Viewing 15 posts - 16 through 30 (of 39 total)

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