sql >> Database teknologi >  >> RDS >> Sqlserver

Anmod om sammenføjning af resultaterne af to andre anmodninger med GROUP BY-klausul i SQL Server 2005

Ja, brug indlejrede forespørgsler:

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

Eller med den nye CTE-syntaks kan du have:

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


  1. Er mysql count(*) meget mindre effektivt end count(specific_field)?

  2. Rails survey stil applikation - Vis alle svar på mulighed

  3. Django og PostgreSQL sekvens til primær nøgle autoincrement

  4. Bestem mime-type fra MySQL-kolonnen