• I agree with Kepr00.

    The question is not whether it CAN be done in SQL but whether it SHOULD be done in SQL. I can and have in the past written some ridiculously complex queries (also for Crystal Reports, which I don't think is a co-incidence). Not one of those queries (and there have been a couple dozen of them) have survived their first encounter with a maintenance programmer.

    Anything that takes an expert the better part of a day to concoct is going to be unintelligable to average programmer. Anything that is unintelligable to the average programmer is going to be broken on a regular basis (and blamed for unrelated problems on an even more regular basis).

    I recommend NOT writing queries this complex. Use simple looping structures and temporary tables in stored procedures.

    Otherwise someone else will spend a week re-writing things in a simple way the first time they blame the undecipherable code for an unrelated problem while you're on vacation.

    My 0.02,

    Steven