2013-09-18 11 views
10

मुझे एकाधिक तालिकाओं में शामिल होने की आवश्यकता है, एक तालिका में एक कॉलम द्वारा अलग-अलग तालिकाओं और समूह से गणना का चयन करें।एकाधिक तालिकाओं में शामिल हों, एक क्वेरी में एक कॉलम द्वारा विभिन्न तालिकाओं और समूह से गणना का चयन करें

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 

या बस

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 

अधिक तालिकाओं नहीं है, कुछ है कि अतिरिक्त आवश्यकता मिलती है ... किसी की मदद कृपया: यह कैसे मैं इस अलग से करना होगा है?

+0

क्या आप उन्हें एक साथ जोड़ नहीं सकते हैं, प्रत्येक समूह को अलग करने के लिए एक स्थिर टेक्स्ट फ़ील्ड जोड़ सकते हैं? – Joe

+0

मैं अभी भी एक बच्चा एसक्यूएल विज़ार्ड हूं;) मैं यूनियन की जांच करूंगा। धन्यवाद! – HLkatie

+0

आपका परिणाम क्या होना चाहिए? विभिन्न प्रश्नों से सभी मायने रखता है? – Lobo

उत्तर

7

अगर मैं आपके सवाल का सही ढंग से समझ, आप इस तरह के पदों, ब्लॉग्स, घटना आदि के रूप में गिना के साथ समुदाय नाम के लिए देख रहे हैं ..

आपके प्रश्नों को व्यक्तिगत रूप से गिनती के रूप में, अन्य के लिए SELECT में डमी स्तंभ जोड़ने गणना करता है और फिर UNION अंत में और 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

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

की तरह दिखाई देगा तो, आप कर सकते हैं GROUP BY नाम और गिनती योग अपने परिणाम प्राप्त करने के

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

देरी के लिए खेद है। मैं मदद के लिए कर्मचारियों पर हमारे एसक्यूएल प्रतिभाओं में से एक से पूछता हूं - लेकिन स्पष्ट रूप से जवाब खुद को खोजने का प्रयास करना चाहता था। यह वही है जो उसने सुझाव दिया! धन्यवाद! – HLkatie

+0

बहुत बहुत धन्यवाद! जब तक मैंने कोशिश नहीं की, मैं 3 घंटे तक विभिन्न तकनीकों की कोशिश कर रहा था! –

0

आप LEFT JOIN का उपयोग कर अपने टेबल कनेक्ट करने के लिए के बारे में सोचा है ? फिर आप एनयूएलएल की जांच कर सकते हैं और गैर-नल मानों को जोड़ सकते हैं।

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 
संबंधित मुद्दे