Max date and associated row data

  • I've been scratching my head all morning about how to do something that at first glance seems easy. I'm losing the plot I think. I could come up with a solution, but using temp tables to do it isn't something I like, so I wondered if you good people had an elegant solution?

    This sets up the data and selects it:

    DECLARE @wostage TABLE (wos_ordref CHAR(8), wos_line INT, wos_startdate SMALLDATETIME)

    DECLARE @woitem TABLE (woi_ordref CHAR(8), woi_line INT, woi_product VARCHAR(40))

    DECLARE @xTOOLS TABLE (tool VARCHAR(40))

    INSERT INTO @wostage

    SELECT 'WO288167', 10, '09-12-2009' UNION ALL

    SELECT 'WO288167', 100, '20/12/2009' UNION ALL

    SELECT 'WO288168', 10, '12/11/2009' UNION ALL

    SELECT 'WO288153', 30, '11/08/2009' UNION ALL

    SELECT 'WO288100', 20, '05/09/2009'

    INSERT INTO @woitem

    SELECT 'WO288167', 50, 'T0001' UNION ALL

    SELECT 'WO288168', 40, 'T0001' UNION ALL

    SELECT 'WO288153', 60, 'T0010' UNION ALL

    SELECT 'WO288100', 30, 'T0010'

    INSERT INTO @xTOOLS

    SELECT 'T0001' UNION ALL

    SELECT 'T0010'

    SELECT woi_product, woi_ordref, wos_startdate

    FROM @woitem JOIN @xTOOLS

    ON woi_product = tool JOIN @wostage

    ON woi_ordref = wos_ordref AND wos_line =

    (SELECT MAX(wos_line)

    FROM @wostage

    WHERE wos_ordref = woi_ordref

    AND wos_line < woi_line)

    ORDER BY woi_product, wos_startdate

    Which returns:

    T0001WO2881682009-11-12 00:00:00

    T0001WO2881672009-12-09 00:00:00

    T0010WO2881532009-08-11 00:00:00

    T0010WO2881002009-09-05 00:00:00

    Just to explain, the @wostage records link to @woitem on the xxx_ordref field, but not on xxx_line. The join here seeks to match up woi_line to the highest wos_line value (don't think you need to know why, but I can explain further if you want).

    What I want, is a list of each tool, with the latest date found in the data and the wo that it relates to. In other words, from the above data, I would want this as my result:

    T0001WO2881672009-12-09 00:00:00

    T0010WO2881002009-09-05 00:00:00

    It seemed easy when I started, but I've spent hours trying to find a slick method without much luck.

    After this bit, the next step is to get the same result from a UNION of two queries, each with the same data structure and holding the same type of data, but one is live and the other is archive. So, @xTOOL will remain the same, but there will be @wostage_live, @wostage_arch, @woitem_live and @woitem_arch.

    Hope that makes sense.

    Regards,

    Mark

  • Try this

    SELECT tool AS product, wos_ordref, wos_startdate

    FROM @xTOOLS

    OUTER APPLY (SELECT TOP 1 wos_ordref,wos_startdate

    FROM @wostage

    JOIN @woitem ON wos_ordref = woi_ordref

    WHERE woi_product = tool

    ORDER BY wos_startdate desc) wos

    ORDER BY tool,wos_startdate

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • mister.magoo (6/28/2010)


    Try this

    SELECT tool AS product, wos_ordref, wos_startdate

    FROM @xTOOLS

    OUTER APPLY (SELECT TOP 1 wos_ordref,wos_startdate

    FROM @wostage

    JOIN @woitem ON wos_ordref = woi_ordref

    WHERE woi_product = tool

    ORDER BY wos_startdate desc) wos

    ORDER BY tool,wos_startdate

    Hi MM,

    Many many thanks for your reply.

    Sadly, I get a syntax errors on that:

    Server: Msg 156, Level 15, State 1, Line 3

    Incorrect syntax near the keyword 'OUTER'.

    Server: Msg 156, Level 15, State 1, Line 7

    Incorrect syntax near the keyword 'ORDER'.

    I'm using T-SQL in Query Analyser on a SQL Server 2000. Is the 'APPLY' a keyword in another version as I'm not familiar with it and it's showing grey in Query Analyser which would suggest that it's not recognised?

  • mmcardle (6/28/2010)


    mister.magoo (6/28/2010)


    Try this

    Hi MM,

    Many many thanks for your reply.

    Sadly, I get a syntax errors on that:

    Server: Msg 156, Level 15, State 1, Line 3

    Incorrect syntax near the keyword 'OUTER'.

    Server: Msg 156, Level 15, State 1, Line 7

    Incorrect syntax near the keyword 'ORDER'.

    I'm using T-SQL in Query Analyser on a SQL Server 2000. Is the 'APPLY' a keyword in another version as I'm not familiar with it and it's showing grey in Query Analyser which would suggest that it's not recognised?

    Oops my bad - hadn't spotted this was SQL 2000 !!!

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • I am a bit busy now to do you a "good" answer, but if you just want something that works but is bad....

    SELECT tool,

    (SELECT TOP 1 wos_ordref

    FROM @wostage

    JOIN @woitem ON wos_ordref = woi_ordref

    WHERE woi_product = tool

    ORDER BY wos_startdate desc) AS wos_ordref,

    (SELECT TOP 1 wos_startdate

    FROM @wostage

    JOIN @woitem ON wos_ordref = woi_ordref

    WHERE woi_product = tool

    ORDER BY wos_startdate desc) AS wos_startdate

    FROM @xTOOLS

    ORDER BY tool,3

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • mister.magoo (6/28/2010)


    I am a bit busy now to do you a "good" answer, but if you just want something that works but is bad....

    SELECT tool,

    (SELECT TOP 1 wos_ordref

    FROM @wostage

    JOIN @woitem ON wos_ordref = woi_ordref

    WHERE woi_product = tool

    ORDER BY wos_startdate desc) AS wos_ordref,

    (SELECT TOP 1 wos_startdate

    FROM @wostage

    JOIN @woitem ON wos_ordref = woi_ordref

    WHERE woi_product = tool

    ORDER BY wos_startdate desc) AS wos_startdate

    FROM @xTOOLS

    ORDER BY tool,3

    Hey MM!

    It's a lot better than anything I've thought of so far, so cheers very much!

  • Don't know if it's the best option or not but eliminating the use of subqueries from the column list should make it perform better.

    SELECT i.woi_product, i.woi_ordref, s.wos_startdate

    FROM @woitem i

    JOIN @xTOOLS ON i.woi_product = tool

    JOIN @wostage s ON woi_ordref = wos_ordref

    INNER JOIN (

    SELECTt2.woi_product

    , MAX(t1.wos_startdate) AS wos_startdate

    FROM@wostage t1

    INNER JOIN @woitem t2 ON t1.wos_ordref = t2.woi_ordref

    GROUP BY t2.woi_product

    ) x ON i.woi_product = x.woi_product

    AND s.wos_startdate = x.wos_startdate

    ORDER BY i.woi_product, s.wos_startdate

    _____________________________________________________________________
    - Nate

    @nate_hughes
  • Thanks for all your help.

    I've adopted a version of MM's solution, even though he wasn't happy with it!

    Performance isn't really an issue for me though, as the code runs as an overnight job and is static during the day. I'll re-visit it when I have more time too.

    Thanks again.

  • I get your desired output just adding GROUP BY woi_product (changes bolded):

    SELECT woi_product, /*woi_ordref,*/ Max(wos_startdate) as wos_stardate

    FROM @woitem JOIN @xTOOLS

    ON woi_product = tool JOIN @wostage

    ON woi_ordref = wos_ordref AND wos_line =

    (SELECT MAX(wos_line)

    FROM @wostage

    WHERE wos_ordref = woi_ordref

    AND wos_line < woi_line)

    GROUP BY woi_product

    ORDER BY woi_product--, wos_startdate

  • dbuendiab (7/1/2010)


    I get your desired output just adding GROUP BY woi_product (changes bolded):

    SELECT woi_product, /*woi_ordref,*/ Max(wos_startdate) as wos_stardate

    FROM @woitem JOIN @xTOOLS

    ON woi_product = tool JOIN @wostage

    ON woi_ordref = wos_ordref AND wos_line =

    (SELECT MAX(wos_line)

    FROM @wostage

    WHERE wos_ordref = woi_ordref

    AND wos_line < woi_line)

    GROUP BY woi_product

    ORDER BY woi_product--, wos_startdate

    Except that you are not displaying the order ref and startdate for each tool...

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • Damn it! You're right. Too fast coding...

  • Viewing 11 posts - 1 through 10 (of 10 total)

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