Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

query build help

Hi,

I have a survey that asks customers about the quality of service they receive from our helpdesk. I need to build a report that averages the response against the following scores -

0  unacceptable
25 very bad
50 Satisfactory
75 Good
100 Excellent

A typical row in my database will show

Date       l response  l fix_time       l competency l
2012.01 l very good l excellent      l very bad      l
2012.01 l very good l satisfactory  l very bad      l
2012.01 l very bad   l good            l good            l

I am struggling to work out how i can build the SQL statement to show this. Can anyone guide me?
0
mr_bungle89
Asked:
mr_bungle89
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
dsackerContract ERP Admin/ConsultantCommented:
What's the score for "very good". You didn't give one.
0
 
mr_bungle89Author Commented:
sorry I made a typo it should read very good in the scores.
0
 
dsackerContract ERP Admin/ConsultantCommented:
Try this:
SELECT  Date,
        AVG(CASE response
            WHEN 'very bad' THEN 25
            WHEN 'satisfactory' THEN 50
            WHEN 'very good' THEN 75
            WHEN 'excellent' then 100
            ELSE 0
        END) AS response_value
FROM YourTable
GROUP BY Date

Open in new window

You can then follow suit for your other columns.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Brian CroweCommented:
You've kind of hampered yourself with bad schema but...

Create a table that maps response to a numeric value then join each column against that.

CREATE TABLE ResponseValue (
   Response nvarchar(50) NOT NULL,
   Value int NOT NULL)

INSERT INTO ResponseValue (Response, Value)
VALUES ('unacceptable', 0)

...repeat inserts for each pair

SELECT myTable.Date,
   myTable.response,
   RV1.Value AS Value1,
   myTable.fix_time,
   RV2.Value AS Value2,
   myTable.competency,
   RV3.Value as Value3,
   RV1.Value + RV2.Value + RV3.Value / 3 AS AvgResponseValue
FROM myTable
LEFT OUTER JOIN ResponseValue AS RV1
   ON myTable.response = RV1.Response
LEFT OUTER JOIN ResponseValue AS RV2
   ON myTable.fix_time = RV2.response
LEFT OUTER JOIN ResponseValue AS RV3
   ON myTable.competency = RV3.response

This also assumes you have good data...what do you want to default the value to if a response is blank?
0
 
mr_bungle89Author Commented:
Thanks all,

I can still redesign the schema. What would you suggest I change it to?
0
 
dsackerContract ERP Admin/ConsultantCommented:
Unless you really have a purpose to segregate your tables and code, the default dbo is fine.
0
 
Brian CroweCommented:
CREATE TABLE Survey (
   SurveyID IDENTITY (int, 1,1),
   UserID int (this is assuming you record a user and can tie it off otherwise ignore)
   SurveyDate datetime NO NULL Default = GETDATE())

CREATE TABL SurveyQuestion (
   SurveyQuestion IDENTITY (int, 1,1),
   Question nvarchar(max))

CREATE TABLE SurveyResponse (
   SurveyResponseID IDENTITY (int, 1,1),
   SurveyID int, --Foreign Key reference to Survey Table
   SurveyQuestionID int,  --FK reference to SurveyQuestion Table
   ResponseValueID int)  --FK reference to ResponseValue Table

CREATE TABLE ResponseValue (
   ResponseValueID IDENTITY (int, 1,1),
   Response nvarchar(50),
   Value int)


Table definition is not executable verbatim but should communicate the idea i hope.

The benefits to the lookup table are that you can use it as a source in your app and not have to worry about hardcoding the values and losing compatibility with the DB.  Putting the questions in a separate table allows for expandability of the questions without adding a new column to the table.  This will also make analysis of the responses  much more powerful.  You could create metrics by question or date or whatever much easier.  BTW I would also include a 'no response' option in the ResponseValue table so they can easily be excluded without skewing any averages.

Of course this could all be overkill for your application...just depends on where you see this going in the future.
0
 
PortletPaulCommented:
each response is presumably a choice from a drop-down hence it should not be necessary to store the nvarchar labels at all, and at the very least I would suggest you store the value, not the label.

Date       l response  l fix_time   l competency l
2012.01 l 75            l 100           l 25               l
2012.01 l 75            l 50             l 25               l
2012.01 l 25            l 75             l 75               l

Thi information goes into a "lookup table" that you use to convert numbers to strings when you need that
0  unacceptable
25 very bad
50 Satisfactory
75 Good
100 Excellent

If you were building for a variety of surveys then I'd certainly agree with a more normalized approach as outlined above.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now