Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Fordern Sie das Zusammenführen der Ergebnisse von zwei anderen Anforderungen mit der GROUP BY-Klausel in SQL Server 2005 an

Sicher, verwenden Sie verschachtelte Abfragen:

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

Oder mit der neuen CTE-Syntax können Sie Folgendes haben:

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