Solved

IF in Oracle function COUNT

Posted on 2013-10-24
3
4,242 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
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
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 31

Expert Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now