Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

mySQL query help sum zero

Posted on 2013-11-12
3
Medium Priority
?
688 Views
Last Modified: 2013-11-12
I have 3 tables:

TABLE-A - groups
GID     Group Name
-----     ------
1          GROUPA
2          GROUPB
3          GROUPC


TABLE-B - questions
QID     Question
-----    --------
1         How do you feel? (Scale 1-10)
2         How old are you? (Scale 1-10)
3        1 + 1? (Scale 1-10)

TABLE-C - questionResponses
RID     QID      GID       Result
-----    --------    --------    --------
1         1            1            2  -
2         1            2            5  -
3         2            3            6
4         3            1            1  -
5         2            1            9  -


I want result like this
QUERY OUTPUT

QID      GID       SUM
--------    --------    --------
1            1            2
2            1            9
3            1            1
1            2            5
2            2            0
3            2            0
1            3            0
2            3            6
3            3            0




This is as far as I have got:
SELECT Q.questionID, G.groupID, COALESCE(SUM(R.questionResponse),0)
FROM questions Q

LEFT JOIN questionResponses R
ON Q.questionID = R.questionID

LEFT JOIN groups G
ON G.groupID = R.groupID

WHERE Q.questionID IN ( 244,245,246 )

GROUP BY Q.questionID, G.groupID

ORDER BY Q.questionID

Open in new window

and the output looks like this:
query output
As you can see I am getting the sum for all 3 questions but it isnt including all 3 groups (should be 9).

Any help?
0
Comment
Question by:Steve Tinsley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
awking00 earned 2000 total points
ID: 39641540
select x.qid, x.gid, sum(nvl(r.result,0)) results
from responses r
right join
(select q.qid, g.gid
 from questions q, groups g) x
on r.qid = x.qid and r.gid = x.gid
group by x.gid, x.qid
order by x.gid, x.qid;
0
 

Author Closing Comment

by:Steve Tinsley
ID: 39641661
Thats brilliant! Think I need to read up on my right joins!
Thanks!
0
 
LVL 32

Expert Comment

by:awking00
ID: 39641778
Thanks. Could have used a left join, too. :-)
select x.qid, x.gid, sum(nvl(r.result,0)) results from
(select q.qid, g.gid
 from questions q, groups g) x
left join
responses r
on r.qid = x.qid and r.gid = x.gid
group by x.gid, x.qid
order by x.gid, x.qid;
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question