• ScottPletcher - Friday, December 8, 2017 9:12 AM

    There's no need to join the group row to itself, so this is another possibility:


    SELECT c.*
    FROM #companies c
    LEFT OUTER JOIN #companies g
      ON c.CompanyStatus <> 'group'
       AND c.GroupNumber = g.GroupNumber
       AND g.CompanyStatus = 'group'
    ORDER BY ISNULL(g.CompanyName, c.CompanyName), c.CompanyName

    There may not be a need, but it is more efficient, so I'll stick with joining the group row to itself.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA