मेरे पास यह तालिका प्रतिक्रिया कोड: है
और यह तालिका निमंत्रण: के लिए है
जबकि मैं इसे प्राप्त करना चाहता हूं:
मेरा प्रश्न:
SELECT
i.eventId
,code.responseCode
,COUNT(i.attendeeResponse) responseCount
FROM invitations i
LEFT JOIN response_codes code
ON code.responseCode = i.attendeeResponse
GROUP BY i.eventId, code.responseCode, i.attendeeResponse;
SQLFiddle
3
SHAKIR SHABBIR
29 नवम्बर 2015, 01:38
3 जवाब
सबसे बढ़िया उत्तर
आपको पहली बार में सभी eventId
s और responseCode
के कार्टेसियन उत्पाद का निर्माण करने की आवश्यकता है (आप बिना किसी शर्त के join
इसे प्राप्त कर सकते हैं):
select c.eventId
, c.responseCode
, count( i.attendeeResponse ) as responseCount
from ( select distinct t1.responseCode
, t2.eventId
from `response_codes` t1
join `invitations` t2 ) c
left join `invitations` i on c.responseCode = i.attendeeResponse and c.eventId = i.eventId
group by c.eventId, c.responseCode;
1
potashin
29 नवम्बर 2015, 00:19
आपको ईवेंट और जिम्मेदारी के सभी संयोजनों को प्राप्त करने के लिए जिम्मेदारी-तालिका में शामिल होने की जरूरत है।
SELECT distinct
i.eventId
,code.responseCode
,case when t.responseCount is null then 0
else t.responsecount end rcount
FROM invitations i
cross JOIN response_codes code
left join
(SELECT i.eventId
,code.responseCode
,COUNT(i.attendeeResponse) responseCount
FROM invitations i
JOIN response_codes code
ON code.responseCode = i.attendeeResponse
group by i.eventid, code.responsecode) t
on t.responsecode =code.responsecode and t.eventid = i.eventid
order by i.eventid, code.responsecode desc
1
Vamsi Prabhala
29 नवम्बर 2015, 00:15
एक और आलसी तरीका हो सकता है:
SELECT B.EVENTID,A.RESPONSECODE,
IFNULL((SELECT COUNT(*) FROM INVITATIONS C WHERE C.EVENTID = B.EVENTID AND C.ATTENDEERESPONSE = A.RESPONSECODE),0) AS 'responseCount'
FROM
RESPONSE_CODES A,
INVITATIONS B
GROUP BY A.RESPONSECODE,B.EVENTID
ORDER BY EVENTID ASC,RESPONSECODE DESC
0
Juan Ruiz de Castilla
29 नवम्बर 2015, 01:11
संबंधित सवाल
नए सवाल
mysql
MySQL एक फ्री, ओपन सोर्स रिलेशनल डेटाबेस मैनेजमेंट सिस्टम (RDBMS) है जो स्ट्रक्चर्ड क्वेरी लैंग्वेज (SQL) का उपयोग करता है। इस टैग को अन्य DBs जैसे SQL Server, SQLite आदि के लिए उपयोग न करें। वे विभिन्न DB हैं जो सभी डेटा का प्रबंधन करने के लिए SQL की अपनी बोलियों का उपयोग करते हैं।