Solved

Getting Percentage Report From Table Data

Posted on 2013-06-06
10
191 Views
Last Modified: 2013-06-07
I have a table and it has information about training taken:
Joe Blow       Training 1        Pass
Joe Blow       Training 2        Fail
Joe Blow       Training 3        Pass
Jane Doe      Training 2         Pass
Jane Doe      Training 3         Fail

From that data, I want to create a table that just has percentages like:
Joe Blow       Passed 66%    Failed 33%
Jane Doe      Passed 50%    Failed 50%

Is there a way to do that from a second query against the first query?

I prefer a SQL query over a stored procedure if possible.

thanks!
0
Comment
Question by:Starr Duskk
[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
  • 6
  • 2
  • 2
10 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39227095
Give this a whirl... (with test script)

DROP TABLE #tmp
GO

CREATE TABLE #tmp (name varchar(10), training varchar(10), score varchar(10))

INSERT INTO #tmp (name, training, score)
VALUES
      ('Joe Blow', 'Training 1', 'Pass'),
      ('Joe Blow', 'Training 2', 'Fail'),
      ('Joe Blow', 'Training 3', 'Pass'),
      ('Jane Doe', 'Training 2', 'Pass'),
      ('Jane Doe', 'Training 3', 'Fail')

SELECT name, 'Passed ' + CAST(pass_count / CAST(total as decimal(5,2)) * 100 as varchar(10)) + '%'
FROM (
SELECT name, SUM(CASE score WHEN 'Pass' THEN 1 WHEN 'Fail' THEN 0 END) as pass_count, COUNT(score) as total
FROM #tmp
GROUP BY name) a
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39227167
... cleaner ...

DROP TABLE #tmp
GO

CREATE TABLE #tmp (name varchar(10), training varchar(10), score varchar(10))

INSERT INTO #tmp (name, training, score)
VALUES
      ('Joe Blow', 'Training 1', 'Pass'),
      ('Joe Blow', 'Training 2', 'Fail'),
      ('Joe Blow', 'Training 3', 'Pass'),
      ('Jane Doe', 'Training 2', 'Pass'),
      ('Jane Doe', 'Training 3', 'Fail')

SELECT
      name,
      'Passed ' + CAST(CAST(pass_count / CAST(total as decimal(3,0)) * 100 as decimal(5,2)) as varchar(10)) + '%' as passed_pct,
      'Failed ' + CAST(CAST((1-pass_count / CAST(total as decimal(3,0))) * 100 as decimal(5,2))  as varchar(10)) + '%' as failed_pct
FROM (
SELECT name, SUM(CASE score WHEN 'Pass' THEN 1 WHEN 'Fail' THEN 0 END) as pass_count, COUNT(score) as total
FROM #tmp
GROUP BY name) a
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 39227174
great! that looks like the right direction. I'll let you know. thanks!
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 49

Expert Comment

by:PortletPaul
ID: 39227935
>>Is there a way to do that from a second query against the first query?
what first query?
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 39228220
jimhorn figured out what I meant and answered.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39228249
Excellent - he's a good guy: award the points and I'll leave you in peace :)
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 39229637
Yes, I will award points when I've tried it and confirmed I have no further questions about it.
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 39230572
Okay, jim, thanks. it's close. But I am not going to be inserting into a table. I will be creating a select query that creates the data that is in :
INSERT INTO #tmp (name, training, score)

So I need it to be something more like:

SELECT name, 'Passed ' + CAST(pass_count / CAST(total as decimal(5,2)) * 100 as varchar(10)) + '%'
FROM (
SELECT name, SUM(CASE score WHEN 'Pass' THEN 1 WHEN 'Fail' THEN 0 END) as pass_count, COUNT(score) as total 
FROM (Select name, training, score from trainingtable)
GROUP BY name) a 

Open in new window


See my FROM line where I use a select clause.

How do I do that?

thanks!
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 39230588
oh shucks. nevermind. let me ask a new question about that after I figure out what I want to ask. thanks!
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 39230691
Thanks for your help!
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

689 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