[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
5
Medium Priority
?
274 Views
Last Modified: 2012-05-10
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
reporte-deseado.JPG
0
Comment
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
  • Learn & ask questions
  • 3
5 Comments
 
LVL 11

Expert Comment

by:JoeNuvo
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

by:JoeNuvo
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

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

Expert Comment

by:lance86
ID: 33583475
an NVL() or DECODE() might do the trick.
0
 
LVL 11

Accepted Solution

by:
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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.…

650 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