Home » SQL & PL/SQL » SQL & PL/SQL » Count of table column
Count of table column [message #293248] Fri, 11 January 2008 06:21 Go to next message
hgriva
Messages: 17
Registered: December 2007
Junior Member
Hi,
i have a table xx with column flg.

it has values like

flg
----
T
G
T
T
G
G
G
G

I want to write a query that would retrieve
count(flg) where flg='G'
and count(flg) where flg='T'
in a single line query.



Re: Count of table column [message #293249 is a reply to message #293248] Fri, 11 January 2008 06:43 Go to previous messageGo to next message
Frank Naude
Messages: 4579
Registered: April 1998
Senior Member
Try something like:

SELECT SUM( DECODE(flg, 'T', 1, 0) ), SUM( DECODE(flg, 'G', 1, 0) ) FROM ...
Re: Count of table column [message #293250 is a reply to message #293249] Fri, 11 January 2008 06:49 Go to previous messageGo to next message
hgriva
Messages: 17
Registered: December 2007
Junior Member
Hi,
Thanks for your reply.
I need one more clarification.

if want count of flg where flg='G' and count of flg <>'G'in single query,how would i get it
Re: Count of table column [message #293252 is a reply to message #293250] Fri, 11 January 2008 07:01 Go to previous messageGo to next message
Frank Naude
Messages: 4579
Registered: April 1998
Senior Member
You can use the same aprouch. If you understood the first quey, this one should be easy:

SUM( DECODE(flg, 'G', 1, 0) )
SUM( DECODE(flg, 'G', 0, 1) )
Re: Count of table column [message #293253 is a reply to message #293252] Fri, 11 January 2008 07:04 Go to previous messageGo to next message
hgriva
Messages: 17
Registered: December 2007
Junior Member
Thanks frank.
I got it.
Re: Count of table column [message #293259 is a reply to message #293253] Fri, 11 January 2008 08:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
If you want to count it is better to use COUNT instead of SUM:
COUNT(DECODE(flg, 'G', 1))
COUNT(DECODE(flg, 'G', null, 1))

Regards
Michel
Re: Count of table column [message #293264 is a reply to message #293259] Fri, 11 January 2008 08:48 Go to previous messageGo to next message
Frank Naude
Messages: 4579
Registered: April 1998
Senior Member
I though SUM would be better - it's 2 characters less to type Smile
Re: Count of table column [message #293268 is a reply to message #293264] Fri, 11 January 2008 08:58 Go to previous message
kir_ait
Messages: 198
Registered: November 2007
Location: Bangalore,India
Senior Member

Frank Naude

Quote:

I though SUM would be better - it's 2 characters less to type


Laughing
Previous Topic: Package compilation: Error: ORA-00980: synonym translation is no longer valid
Next Topic: Is Primary Key Function
Goto Forum:
  


Current Time: Thu Apr 25 20:22:23 CDT 2024