Solved

IF in Oracle function COUNT

Posted on 2013-10-24
3
5,565 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 49

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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

691 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