Link to home
Start Free TrialLog in
Avatar of enrique_aeo
enrique_aeo

asked on

query resumen

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
Avatar of JoeNuvo
JoeNuvo
Flag of Viet Nam image

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
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
Avatar of enrique_aeo
enrique_aeo

ASKER

it is working, But, instead of null 0 I want it...
null.JPG
an NVL() or DECODE() might do the trick.
ASKER CERTIFIED SOLUTION
Avatar of JoeNuvo
JoeNuvo
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial