[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# query resumen

Posted on 2010-09-01
Medium Priority
274 Views
Hol experts, I have a table inthe database with 3 columns (observanciaRegistro, manejoGramatical, utilizacionFormalismos).
Each of these columns can have the values 1-5.
I need to do a query to tell me the number of times under the number 1, number 2, etc
I attached two files data and query
number.JPG
0
Question by:enrique_aeo
[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
• 3

LVL 11

Expert Comment

ID: 33583208
Replace [tablename] with name of your table.

SELECT * FROM
(
SELECT 'observanciaRegistro' as [Title], observanciaRegistro as [Score], Count(*) as CNT FROM [tablename] GROUP BY observanciaRegistro
UNION ALL
SELECT 'manejoGramatical' , manejoGramatical, Count(*) FROM [tablename] GROUP BY manejoGramatical
UNION ALL
SELECT 'utilizacionFormalismos', utilizacionFormalismos, Count(*) FROM [tablename] GROUP BY utilizacionFormalismos
) As SubQuery
PIVOT (SUM(CNT) FOR [Score] IN ([1],[2],[3],[4],[5])) P
0

LVL 11

Expert Comment

ID: 33583214
p.s. in order to get result same as your image, you may need to change

PIVOT (SUM(CNT) FOR [Score] IN ([1],[2],[3],[4],[5])) P

to be

PIVOT (SUM(CNT) FOR [Score] IN ([5],[4],[3],[2],[1])) P
0

Author Comment

ID: 33583249
it is working, But, instead of null 0 I want it...
null.JPG
0

Expert Comment

ID: 33583475
an NVL() or DECODE() might do the trick.
0

LVL 11

Accepted Solution

JoeNuvo earned 1400 total points
ID: 33583588
Just in case nobody else provide better solution.
Here is the simple fixed to replace NULL with zero

SELECT
[Title],
COALESCE([5],0) as [5],
COALESCE([4],0) as [4],
COALESCE([3],0) as [3],
COALESCE([2],0) as [2],
COALESCE([1],0) as [1]
FROM
(
SELECT * FROM
(
SELECT 'observanciaRegistro' as [Title], observanciaRegistro as [Score], Count(*) as CNT FROM [tablename] GROUP BY observanciaRegistro
UNION ALL
SELECT 'manejoGramatical' , manejoGramatical, Count(*) FROM [tablename] GROUP BY manejoGramatical
UNION ALL
SELECT 'utilizacionFormalismos', utilizacionFormalismos, Count(*) FROM [tablename] GROUP BY utilizacionFormalismos
) As SubQuery
PIVOT (SUM(CNT) FOR [Score] IN ([1],[2],[3],[4],[5])) As SubPivot
) As Result
0

## Featured Post

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
###### Suggested Courses
Course of the Month13 days, 4 hours left to enroll