This my dataset and i want to find No. of gender
name|gender|
----+------+
AA |Male |
BB |Male |
CC |Male |
DD |Female|
EE |Female|
sql query :
select
gender,count(*)
from
schoolofit.user_request_1
group by gender
result:
gender|count|
------+-----+
Female| 2|
Male | 3|
But i am looking for below result format
gender-1 | gender-2
-------------------
Male | Female
3 | 2
Note : looking for sql query with and without using Union
4
Answers
I understand that you want to convert row values of your current query result to columns
This can be achieved using CASE STATEMENT :
Update :
Based on the input and clarification :
Here is the SQLFiddle; where I have used data provided by you in post.
You can do it using
filter
clause :CAST
count to varchar to be able to union the gender count with the first row (male, female).Demo here
When the values can be dynamic, and not just ‘Male’ or ‘Female’ you can do:
see: DBFIDDLE
One option to avoid using a union, you can use an array then unnest it as the following:
Demo