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
Solved

IF in Oracle function COUNT

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exchange 2013 tmp files 3 41
Help with a script Updating records from other records in the Same table 13 49
Perl Versus AWK? 7 49
Where on a calculated field 1 21
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

856 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