My 1000th post!I thought this would be trivially easy but maybe not....the basic outline is you would chain the CTE like thisWITH xxx AS (), yyy AS ()SELECT ......
However I'm not so sure about your data. I came up with this (you might not need the full outer join... My CTE's are trivially simple but the same principal should apply You can run to test output.DECLARE @foo_X TABLE ( [Id] INT , [Comp] INT )DECLARE @foo_Y TABLE ( [Id] INT , [Comp] INT )INSERT @foo_X ([Id], [Comp]) SELECT 1, 10UNION ALL SELECT 1, 23UNION ALL SELECT 2, 30UNION ALL SELECT 3, 20UNION ALL SELECT 3, 20INSERT @foo_Y([Id], [Comp]) SELECT 1, 123UNION ALL SELECT 2, 2123UNION ALL SELECT 2, 323UNION ALL SELECT 2, -10UNION ALL SELECT 5, 10UNION ALL SELECT 6, 20;WITH tabX ([Id], [Comp], [axis]) AS ( SELECT [Id], [Comp], 'x' FROM @foo_X ), tabY([Id], [Comp], [axis]) AS ( SELECT [Id], [Comp], 'y' FROM @foo_Y )SELECT [ID] , COUNT(CASE [axis] WHEN 'x' THEN [comp] ELSE NULL END) AS [xCount] , MIN(CASE [axis] WHEN 'x' THEN [comp] ELSE NULL END) AS [xMin] , COUNT(CASE [axis] WHEN 'y' THEN [comp] ELSE NULL END) AS [yCount] , MIN(CASE [axis] WHEN 'y' THEN [comp] ELSE NULL END) AS [yMin]FROM ( SELECT x.[ID] AS [Id] , [comp] AS [comp] , [axis] AS [axis] FROM tabX x UNION ALL SELECT y.[ID] AS [Id] , [comp] AS [comp] , [axis] AS [axis] FROM tabY y ) CombinedGROUP BY [Id]
Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION