| Author |
Topic |
|
patelhasu
Starting Member
13 Posts |
Posted - 2002-02-25 : 16:40:13
|
| i have this table which has 4 fields. Id, Name, Gender, Grade.I want to create a crosstab query using stored procedure so it would crosstab by genderi.e. Name Id Male FemaleGrade:04 21 31Please help me.. |
|
|
robvolk
Most Valuable Yak
15732 Posts |
|
|
patelhasu
Starting Member
13 Posts |
Posted - 2002-02-26 : 11:20:14
|
| This is what i did but i get an error "The sum or average aggregate operation cannot take a char data type as an argument."SELECT Demographic.Id,Sum(CASE Demographic.Gender WHEN 'F' THEN Gender ELSE 0 END) Female, Sum(CASE Demographic.Gender WHEN 'M' THEN Gender ELSE 0 END) Males, Sum(Gender) TotalFROM DemographicGROUP BY Demographic.Id |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2002-02-26 : 11:33:56
|
| Use Max() or Min() instead of Sum() for non-numeric data types (char, varchar, datetime, etc.) |
 |
|
|
patelhasu
Starting Member
13 Posts |
Posted - 2002-02-26 : 11:40:41
|
| I'm so sorry to bug you so much but how does the Min() or Max() work.Thank you so much for helping me with this.I really appreciate it. |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2002-02-26 : 11:48:18
|
| Min will find the smallest value in a set of values, Max will find the largest value. The other aggregate functions (Sum, Avg, Var, Std) can only use numeric arguments because they are arithmetic operations. Min and Max can work on any datatype.I'm not clear on what you're trying to accomplish with this cross tab. Are you counting, summarizing, or simply displaying rows? Can you show EXACTLY how your source table looks, with some sample data, and EXACTLY how you want to output to appear? It's hard to know if this will work without some guidance. |
 |
|
|
patelhasu
Starting Member
13 Posts |
Posted - 2002-02-26 : 11:54:14
|
| Okay i have this table called Demographic There are field Id, Grade, Genderi.e. Id Grade Gender1111 04 F2222 05 M3333 05 F4444 03 MThe output should be: Grade Male Female 04 0 1 05 1 1 03 1 0Thank You so much for your help.Hasu |
 |
|
|
James Tow
Starting Member
2 Posts |
Posted - 2002-02-26 : 12:12:36
|
| I think you were close on the original. Why not try:SELECTGRADE,MALE = SUM(CASE WHEN GENDER = 'M' THEN 1 ELSE 0 END),FEMALE = SUM(CASE WHEN GENDER = 'F' THEN 1 ELSE 0 END)FROM DEMOGRAPHICGROUP BY GRADE |
 |
|
|
patelhasu
Starting Member
13 Posts |
Posted - 2002-02-26 : 12:22:59
|
| Thank You so muchIt worked really goodThanks |
 |
|
|
jvolpe
Starting Member
2 Posts |
Posted - 2005-09-01 : 11:44:05
|
| One other question. Let's say you want to pivot this info...Name EmailJason V jason.volpe@x.comJason V jason.volpe@google.comJason V jason.volpe@msn.comHow do you get the email addresses into Email1 Email2 Email3, etc columns without knowing the previous email for each column. I'm wanting to do this in a view. |
 |
|
|
|