Unix Technical Forum

How to calculate the total of a varchar column?

This is a discussion on How to calculate the total of a varchar column? within the Oracle Database forums, part of the Database Server Software category; --> Hello, I have a column named Gender in my table. Its a varchar2 type column and each row is ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-25-2008, 02:16 AM
Krishna
 
Posts: n/a
Default How to calculate the total of a varchar column?

Hello,

I have a column named Gender in my table. Its a varchar2 type column
and each row is either 'M' or and 'F'. I want to count the no. of Ms
and number of Fs and obtain the result of the number of Ms and Fs in a
separate row.

Right now I am only able to perform the count function and obtain the
number of Ms and Fs. But I am not able to get the total for Ms and Fs.
So can some one please tell me how should I do it?

Right now I am giving the following command:

SELECT GENDER, COUNT(GENDER) As American_Indian
FROM STUDENT_DETAIL
WHERE ETHNIC_GROUP_CODE = 1
GROUP BY GENDER ORDER BY GENDER DESC

And the output is coming as

Gender American_Indian
-----------------------------------------
M 60
F 60

What I want to see is

Gender American_Indian
-----------------------------------------
M 60
F 60
MF 120

Please help.

Thank you.

Krishna.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-25-2008, 02:16 AM
isapmon@terra.es
 
Posts: n/a
Default Re: How to calculate the total of a varchar column?

Hi:
you can use an union:

SELECT GENDER, COUNT(GENDER) As American_Indian
FROM STUDENT_DETAIL
WHERE ETHNIC_GROUP_CODE = 1
GROUP BY GENDER
union
SELECT 'MF',COUNT(*)
FROM STUDENT_DETAIL
WHERE ETHNIC_GROUP_CODE = 1 AND GENDER IN ('M','F')
ORDER BY GENDER DESC

I hope it's useful to you.
Regards,
Isa

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-25-2008, 02:16 AM
Krishna
 
Posts: n/a
Default Re: How to calculate the total of a varchar column?

Thanks a lot Isa,

That really helps. Have a great day.

Regards,

Krishna.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-25-2008, 02:16 AM
adik_q@wp.pl
 
Posts: n/a
Default Re: How to calculate the total of a varchar column?

Hi,

.... a bit shorter version

SELECT COUNT(gender), gender, NVL(gender, 'ALL') FROM test_gender
GROUP BY GROUPING SETS(gender,0)

Regards
Adrian

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-25-2008, 02:16 AM
adik_q@wp.pl
 
Posts: n/a
Default Re: How to calculate the total of a varchar column?

....correction
SELECT COUNT(gender), NVL(gender, 'ALL') a FROM Student_detail
WHERE ethnic_group_code = 1
GROUP BY GROUPING SETS(gender,0) ORDER BY a DESC

Best regards,
Adrian

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-25-2008, 02:21 AM
Krishna
 
Posts: n/a
Default Re: How to calculate the total of a varchar column?

Hi,

Thanks a lot all for your help. Now I need to interchange the output
slightly.
Instead of this

Gender American_Indian
-----------------------------------------
M 60
F 60
MF 120

I want the output as
Race Male Female
-------------------------------------
Race 1 60 60
Race 2 70 50

I am getting it separately for each column but I am not able to show
Male and Female as 2 separate columns.

I've tried the following:


SELECT DISTINCT
DECODE(GENDER, 'M',
COUNT(GENDER),NULL) MALE
FROM STUD_DTL
GROUP BY GENDER;

Not executing giving errors


SELECT COUNT(GENDER) MALE and COUNT(GENDER) FEMALE
FROM STUD_DTL WHERE GENDER = 'M' AND GENDER = 'F'
ORDER BY GENDER;

SELECT COUNT(GENDER) MALE, COUNT(GENDER) FEMALE
FROM STUD_DTL WHERE GENDER = 'M' AND GENDER = 'F'
ORDER BY GENDER;

None of the above commands is working. Can you please help me?

Thank you.

Krish.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-25-2008, 02:21 AM
Michel Cadot
 
Posts: n/a
Default Re: How to calculate the total of a varchar column?


"Krishna" <mpkrishna25@yahoo.com> a écrit dans le message de news: 1140044911.332349.77810@g44g2000cwa.googlegroups.c om...
| Hi,
|
| Thanks a lot all for your help. Now I need to interchange the output
| slightly.
| Instead of this
|
| Gender American_Indian
| -----------------------------------------
| M 60
| F 60
| MF 120
|
| I want the output as
| Race Male Female
| -------------------------------------
| Race 1 60 60
| Race 2 70 50
|
| I am getting it separately for each column but I am not able to show
| Male and Female as 2 separate columns.
|
| I've tried the following:
|
|
| SELECT DISTINCT
| DECODE(GENDER, 'M',
| COUNT(GENDER),NULL) MALE
| FROM STUD_DTL
| GROUP BY GENDER;
|
| Not executing giving errors
|
|
| SELECT COUNT(GENDER) MALE and COUNT(GENDER) FEMALE
| FROM STUD_DTL WHERE GENDER = 'M' AND GENDER = 'F'
| ORDER BY GENDER;
|
| SELECT COUNT(GENDER) MALE, COUNT(GENDER) FEMALE
| FROM STUD_DTL WHERE GENDER = 'M' AND GENDER = 'F'
| ORDER BY GENDER;
|
| None of the above commands is working. Can you please help me?
|
| Thank you.
|
| Krish.
|

select sum(decode(gender,'M',1,0)) male,
sum(decode(gender,'F',1,0)) female,
sum(decode(gender,'M',0,'F',0,1)) other,
count(*) total
from stud_dtl
/

Regards
Michel Cadot


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-25-2008, 02:25 AM
Janek
 
Posts: n/a
Default Re: How to calculate the total of a varchar column?

In article <1139846992.606755.128210@g47g2000cwa.googlegroups .com>,
mpkrishna25@yahoo.com says...
> Hello,
>
> I have a column named Gender in my table. Its a varchar2 type column
> and each row is either 'M' or and 'F'. I want to count the no. of Ms
> and number of Fs and obtain the result of the number of Ms and Fs in a
> separate row.
>
> Right now I am only able to perform the count function and obtain the
> number of Ms and Fs. But I am not able to get the total for Ms and Fs.
> So can some one please tell me how should I do it?
>
> Right now I am giving the following command:
>
> SELECT GENDER, COUNT(GENDER) As American_Indian
> FROM STUDENT_DETAIL
> WHERE ETHNIC_GROUP_CODE = 1
> GROUP BY GENDER ORDER BY GENDER DESC
>
> And the output is coming as
>
> Gender American_Indian
> -----------------------------------------
> M 60
> F 60
>
> What I want to see is
>
> Gender American_Indian
> -----------------------------------------
> M 60
> F 60
> MF 120
>
> Please help.
>


select GENDER, COUNT(GENDER)
from...
group by rollup(gender)

Regards,
Jan
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:19 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com