• mickyT (9/3/2012)


    You could try the following query.

    select f.Path, f.FolderName, f.FolderSize, d.FileName, d.FileSize

    from @Folders f

    left outer join @Files d on f.Path = d.Path

    group by grouping sets ((f.Path, Foldername, FolderSize), (f.path, d.FileName, d.FileSize))

    order by f.Path, d.filename

    This produces the same result except there are NULLs rather than empty strings

    It does, but at a cost - each table is scanned twice and there are two sorts. The UNION version scans each table once followed by a single sort. Kudos for lateral thinking though ๐Ÿ˜Ž

    โ€œ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