query build help

Posted on 2013-05-24
Last Modified: 2013-05-24

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?
Question by:mr_bungle89
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
  • 3
  • 2
  • 2
  • +1
LVL 20

Expert Comment

ID: 39195100
What's the score for "very good". You didn't give one.

Author Comment

ID: 39195109
sorry I made a typo it should read very good in the scores.
LVL 20

Accepted Solution

dsacker earned 250 total points
ID: 39195126
Try this:
        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

Open in new window

You can then follow suit for your other columns.
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

LVL 34

Assisted Solution

by:Brian Crowe
Brian Crowe earned 250 total points
ID: 39195143
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,
   RV1.Value AS Value1,
   RV2.Value AS Value2,
   RV3.Value as Value3,
   RV1.Value + RV2.Value + RV3.Value / 3 AS AvgResponseValue
FROM myTable
   ON myTable.response = RV1.Response
   ON myTable.fix_time = RV2.response
   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?

Author Comment

ID: 39195149
Thanks all,

I can still redesign the schema. What would you suggest I change it to?
LVL 20

Expert Comment

ID: 39195152
Unless you really have a purpose to segregate your tables and code, the default dbo is fine.
LVL 34

Expert Comment

by:Brian Crowe
ID: 39195187
   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.
LVL 48

Expert Comment

ID: 39196141
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.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

751 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