Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Solicitação de junção dos resultados de duas outras solicitações com cláusula GROUP BY no SQL Server 2005


Claro, use consultas aninhadas:
select *
from (select count(*) as delivery_count, clientid 
      from deliveries group by clientid) AS view1
inner join (select count(*) as action_count, clientid
            from routeactions group by clientid) AS view2
    on view1.clientid = view2.clientid

Ou com a nova sintaxe CTE você pode ter:
WITH view1 AS (
    select count(*) as delivery_count, clientid from deliveries group by clientid
), view2 AS (
    select count(*) as action_count, clientid from routeactions group by clientid
)
select * from view1 inner join view2 on view1.clientid = view2.clientid