• eobiki10 (12/23/2013)


    Thanks Chris. Your suggestion works perfectly fine. The APPLY is really the magic wow!!!

    EO

    Excellent. There's a lesson here - know your data. If you knew in advance of writing your query that most of your tables join together as one-to-one, you would have figured it out.

    APPLY isn't key here - you could have done the same job with a joined derived table. Similar to the APPLY block but you return the key column in the subquery - in addition to the aggregated columns - then use it in the join.

    You should investigate this:

    isnull((Select top 1 'Y'

    from bo_live5.dbo.hba_source_bus AS ii

    where ii.matter_uno=i.matter_uno and ii.comments LIKE '%walk-in%'),'N') AS [Walk In]

    It doesn't make sense.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden