Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Every One,

I am working in asp.net. I have a table MyClass. In this I have three columns.
Columns are classname, gender, house.

records are fill in following format:
ClassName Gender House
abc Male Rabbit
abc male bull
abc female rabbit
kbc male rabbit
kbc female bull
kbc male rabbit


My requirement is that i want to show records in following format.



classname, Male , Female, House
abc 1 1 rabbit
abc 1 0 bull
kbc 2 0 rabbit
kbc 0 1 bull

classname, Male , Female, House
will be column name in ouput. please help me that what query will be make for this.
Posted
Updated 9-Jul-12 1:25am
v2
Comments
Sandeep Mewara 9-Jul-12 7:12am    
This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.

What does Male/Female column values as 10 & 5 means? :doh:
[no name] 9-Jul-12 7:13am    
"please help me that what query will be make for this"... instead of asking for some to do this for you, why not try something yourself? This is pretty much the most simple of SQL statements you can get.

Hi..

Please use the below query.

It will help u


SQL
select [ClassName],[Male],[female],[House]
from
(select * from tblex)source pivot
(count(Gender) for Gender in ([Male],[female]))pt

order by ClassName
 
Share this answer
 
Comments
Arul R Ece 9-Jul-12 7:42am    
If it is help u means , accept this as answer
coolnavjot31 9-Jul-12 7:50am    
Thanks it is working for me!
AshishChaudha 9-Jul-12 7:52am    
Nice...my 4!
Arul R Ece 9-Jul-12 8:01am    
Ok.....Thanks ..
Let's Try with this ...................

SQL
select ClassName
, SUM(case when gender = 'Male' then 1 else 0 end) as Male
, SUM(case when gender = 'Female' then 1 else 0 end) as Female 
, House 
from t1 
group by ClassName,House


OUTPUT
--------------------------------------------------
CalssName     Male    Female   House
--------------------------------------------------
abc	        1	0	bull
kbc	        0	1	bull
abc	        1	1	Rabbit
kbc	        2	0	Rabbit
--------------------------------------------------
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900