2013-09-18 24 views
10

Ho bisogno di unire più tabelle, selezionare i conteggi da tabelle diverse e raggruppare per una colonna in una query. Questo è come farei questo a parte:Unire più tabelle, selezionare i conteggi da diverse tabelle e raggruppare per una colonna in una query

select  c.CommunityName, SUM(case when m.ListKey = c.ListKey then 1 else 0 end) as Posts 
from  Community c with(NOLOCK) 
join  messages_ m with(NOLOCK) 
on   c.ListKey = m.ListKey 
group by c.CommunityName 

select  c.CommunityName, SUM(case when b.CommunityKey = c.CommunityKey then 1 else 0 end) as Blogs 
from  Community c with(NOLOCK) 
join  Blog b with(NOLOCK) 
on   c.CommunityKey = b.CommunityKey 
group by c.CommunityName 

select  c.CommunityName, SUM(case when ce.CommunityKey = c.CommunityKey then 1 else 0 end) as Events 
from  Community c with(NOLOCK) 
join  CalendarEvent ce with(NOLOCK) 
on   c.CommunityKey = ce.CommunityKey 
where  ce.StartDateTime >= GETDATE() 
group by c.CommunityName 

o semplicemente

select  c.CommunityName, COUNT(*) 
from  Community c with(NOLOCK) 
join  messages_ m with(NOLOCK) 
on   c.ListKey = m.ListKey 
group by c.CommunityName 

select  c.CommunityName, COUNT(*) 
from  Community c with(NOLOCK) 
join  Blog b with(NOLOCK) 
on   c.CommunityKey = b.CommunityKey 
group by c.CommunityName 

select  c.CommunityName, COUNT(*) 
from  Community c with(NOLOCK) 
join  CalendarEvent ce with(NOLOCK) 
on   c.CommunityKey = ce.CommunityKey 
where  ce.StartDateTime >= GETDATE() 
group by c.CommunityName 

Non ci sono più tabelle, alcuni che richiedono ulteriore unisce ... Per favore qualcuno può aiutarmi?

+0

Non è possibile unirle tutte insieme, aggiungendo un campo di testo statico per distinguere ciascun gruppo? – Joe

+0

Sono ancora un baby wizard SQL;) Controllerò UNION. Grazie! – HLkatie

+0

Quale dovrebbe essere il risultato? somma di tutti i conteggi di domande diverse? – Lobo

risposta

7

Se ho capito bene la tua domanda, siete alla ricerca di nome della comunità insieme con i conteggi, come i messaggi, blog, eventi ecc ..

come Conte individualmente le vostre domande, aggiungere colonne fittizie nel SELECT per l'altro conta e poi alla fine UNION e ottiene il SUM.

SELECT CommunityName , SUM(MessageCount), SUM(BlogCount), SUM(EventCount) 
FROM (
    SELECT  c.CommunityName CommunityName , COUNT(*) MessageCount, 0 BlogCount, 0 EventCount 
    FROM  Community c with(NOLOCK) 
    JOIN  messages_ m with(NOLOCK) 
    ON   c.ListKey = m.ListKey 
    GROUP BY c.CommunityName 

    UNION 

    SELECT  c.CommunityName, 0, COUNT(*), 0 
    FROM  Community c with(NOLOCK) 
    JOIN  Blog b with(NOLOCK) 
    ON   c.CommunityKey = b.CommunityKey 
    GROUP BY c.CommunityName 

    UNION 

    SELECT  c.CommunityName, 0, 0, COUNT(*) 
    FROM  Community c with(NOLOCK) 
    JOIN  CalendarEvent ce with(NOLOCK) 
    ON   c.CommunityKey = ce.CommunityKey 
    WHERE  ce.StartDateTime >= GETDATE() 
    GROUP BY c.CommunityName 
) CountsTable 
GROUP BY CountsTable.CommunityName 

CountsTable sarà simile

| COMMUNITYNAME | MESSAGECOUNT | BLOGCOUNT | EVENTCOUNT | 
|---------------|--------------|-----------|------------| 
|   Name |   10 |   0 |   0 | 
|   Name |   0 |  20 |   0 | 
|   Name |   0 |   0 |   30 | 

Quindi, si può GROUP BY nome e riassumere i conteggi per ottenere il risultato

| COMMUNITYNAME | MESSAGECOUNT | BLOGCOUNT | EVENTCOUNT | 
|---------------|--------------|-----------|------------| 
|   Name |   10 |  20 |   30 | 
+0

Ci scusiamo per il ritardo. Alla fine ho chiesto a uno dei nostri geni SQL di chiedere aiuto allo staff, ma ovviamente volevo provare a trovare la risposta da solo. Questo è esattamente ciò che ha suggerito! Grazie! – HLkatie

+0

Grazie mille! Sono rimasto bloccato a provare tecniche diverse per 3 ore fino a quando ho provato questo! –

0

Avete pensato di utilizzare LEFT JOIN per collegare le tabelle ? Quindi puoi controllare i valori NULL e sommare i valori non NULL.

SELECT 
    c.CommunityName, 
    SUM(case when m.ListKey IS NOT NULL then 1 else 0 end) as Posts, 
    SUM(case when b.CommunityKey IS NOT NULL then 1 else 0 end) as Blogs, 
    SUM(case when ce.CommunityKey IS NOT NULL then 1 else 0 end) as Events 
FROM 
    Community c WITH(NOLOCK) 
     LEFT JOIN 
    messages_ m WITH(NOLOCK) 
     ON c.ListKey = m.ListKey 
     LEFT JOIN 
    Blog b WITH(NOLOCK) 
     ON c.CommunityKey = b.CommunityKey 
     LEFT JOIN 
    CalendarEvent ce WITH(NOLOCK) 
     ON c.CommunityKey = ce.CommunityKey 
WHERE 
    ce.StartDateTime >= GETDATE() 
GROUP BY 
    c.CommunityName 
Problemi correlati