Home Forums SQL Server 2008 T-SQL (SS2K8) SQL Query to select one row and fill missing columns from another matching row. RE: SQL Query to select one row and fill missing columns from another matching row.

  • kk 93815 (10/2/2012)


    Thanks laurie and dwain.c.

    dwain.c i like your approach as it's bit simple but please can you give some explanation so that it makes more sense to me... i always thought that max() function is used for numbers...

    did a quick test and both of them seem to work but unfortunately I can't use SQL 2008, sorry didn't mention this earlier.

    Regards,

    kk

    Laurie's answer is correct on MAX. Remember that character strings also have an inherent collation sequence, which is used to resolve MAX/MIN.

    Laurie's double LEFT JOIN and using COALESCE may be better at resolving multiple record ties, unfortunately I don't think it will work if there happens to be 4 records that are all tied.

    I meant to say originally that you probably should try both approaches across a wider range of test data to see which works better for you. Often when you do that, you'll find there are issues and then you can repost those cases so someone can suggest how to best handle them.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St