Solved

IF in Oracle function COUNT

Posted on 2013-10-24
3
5,374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
database connection error mysql stops 7 79
Sorting a SQL script 5 34
oracle query 4 26
What is needed to become a DBA? 7 46
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

738 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