• DavidDroog (7/23/2015)


    Hi All

    Surprised this thread was still going when logged in this morning. ChrisM - I didn't realise people were actually offering to optimise my query, but if that is the case I gratefully accept for 2 reasons.

    1. As a developer I am always on the look-out for better ways of doing things and would be very happy if I was shown the light.

    2. I am somewhat cynical that some of more idealistic statements can actually survive the real world (eg no dynamic sql, no temp tables). I know my code has been universally bagged, but I actually think it is pretty good and it performs a complex function accurately and reasonably quickly. I therefore would like to issue a challenge to anyone who believes they can improve on it to submit their code here (by improve I essentially mean speed which is quantifiable - changing join types etc with no performance improvement is pointless as far as I am concerned).

    If anyone is willing to take the challenge I will post a database with some sample data (I assume I can attach a bak file here).

    Now the code I submitted before was just the output from a subquery for the full proc, so if you think that was complex wait until you see the full thing which is posted below, however I am posting a requirement description so you do not have to follow my code at all, just re-produce the results from my beast. I should also state this is a real world proc in development, so has not gone through testing yet, but I do not anticipate it to change much (unless as stated someone shows me the light!)

    So who is up for the challenge? - I will quite happily eat humble pie if my cynicism is proven false.

    ...

    Absolutely. Can you make a start by providing these three queries, please:

    1.Clients with “disciplines” filtered by geography using the geography parameter. This query should reference client, client address and geography tables plus those required to identify "disciplines". If any other tables are referenced in the query then provide an explanation. If any tables are referenced more than once then provide a full justification.

    2.Identify how to distinguish between existing and potential targets, but keep this separate from query 1 for now. It might be as simple as a grab from sales tables using clients identified by Query 1.

    3.One rep with “disciplines” and products including info to determine “high enough product profile”, chosen using the rep parameter. If any table is referenced more than once in this query, then provide a full justification for it.

    “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