Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySQL query help to display 'missing' values when comparing All Clients' data to one Clients data

Status
Not open for further replies.

Jacque

Technical User
Nov 9, 2001
301
US
My query is set to compare All Clients survey data (likert scale) to a specific Client's survey data.
My current query unions the data - no problem.
The problem begins when I'm comparing the scale value aka Response: The All clients query returns data for all or most of the values but the specific Clients data may only have values for some of the scale values.
The scale values I'm interested in are 1 - 5.

This is what I have currently:

Client Question Response NumOfResponses Client Question Response NumOfResponses
All Question1 1 10 Client150 Question1 1 2
All Question1 2 25 Client150 Question1 2 5
All Question1 3 40 Client150 Question1 4 13
All Question1 4 100 Client150 Question1 5 25
All Question1 5 210

This is what I want:

Client Question Response NumOfResponses Client Question Response NumOfResponses
All Question1 1 10 Client150 Question1 1 2
All Question1 2 25 Client150 Question1 2 5
All Question1 3 40 Client150 Question1 3 0
All Question1 4 100 Client150 Question1 4 13
All Question1 5 210 Client150 Question1 5 25

Because I need to display them in a report I'd like to include for Client150, Question1, Response 3 with zero(0) count but can't seem to figure out how to do that in my query.

Here's a generic short form of my query

set @Client = 'Client150';

SELECT
'All' as Client,
Question,
Response,
Count(Response) as NumOfResponses
from
SurveyData sd
INNER JOIN Clients c
on sd.ClientCode = c.ClientCode
WHERE c.Reported = 1
and sq.Response not in ('',0)
and (sd.SurveyDate >= DATE_ADD(DATE_ADD(CURDATE(),INTERVAL -DAY(CURDATE())+1 DAY), INTERVAL -6 MONTH)
and sd.SurveyDate <= Last_day(DATE_ADD(CURDATE(),INTERVAL -1 MONTH)))
Group by Client, Question, Response
UNION
SELECT
Client,
Question,
Response,
Count(Response) as NumOfResponses
from
SurveyData sd
INNER JOIN Clients c
on sd.ClientCode = c.ClientCode
WHERE c.Reported = 1
and Client = @Client
and sq.Response not in ('',0)
and (sd.SurveyDate >= DATE_ADD(DATE_ADD(CURDATE(),INTERVAL -DAY(CURDATE())+1 DAY), INTERVAL -6 MONTH)
and sd.SurveyDate <= Last_day(DATE_ADD(CURDATE(),INTERVAL -1 MONTH)))
Group by Client, Question, Response;


Thanks in advance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top