Solved

IF in Oracle function COUNT

Posted on 2013-10-24
3
5,231 Views
Last Modified: 2013-10-27
Hi,

I am writing a SQL query in Oracle and I have a column with COUNT function. Now I want to have IF statement in Oracle function COUNT e.g. count (IF auditstr='Login'). Then only it should count.

Please help in that.

Regards,
Baber.
0
Comment
Question by:baberamin
3 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39596892
You use a case expression inside the parentheses, like this:

SELECT
     count (CASE WHEN auditstr='Login' THEN 1 ELSE NULL END)
....

or
SELECT
     count (CASE WHEN auditstr='Login' THEN auditstr ELSE NULL END)
....

BUT, please note count actually counts "the existence of a value" so the following would be exactly the same:

SELECT
     count (CASE WHEN auditstr='Login' THEN 'x' ELSE NULL END)
....

& you may leave out the "else null" if you like.

SELECT
     count (CASE WHEN auditstr='Login' THEN 1 END)
....
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39596978
this might be overly simplistic, but also check if your "IF" could simply be a WHERE clause.


select count(*) from your_table where auditstr = 'Login'

or maybe a group by


select auditstr,count(*) from your_table
group by auditstr

this will show you Login and as well as other values. That may or may not be helpful.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39598571
What does you current query with the count function look like?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Accessing variables in MySQL query 4 52
MySQL-Design Help 12 38
Access control a form field in Lotus Notes 3 24
scheduler notification 9 27
Creating and Managing Databases with phpMyAdmin in cPanel.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to recover a database from a user managed backup

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question