Solved

query resumen

Posted on 2010-09-01
5
263 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 350 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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