Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3239282
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:57:06+00:00 2026-05-17T17:57:06+00:00

I have following query Select a,b,c, case totalcount when 0 then 0 else abcd/totalcount

  • 0

I have following query

Select
        a,b,c,
        case totalcount
            when 0 then 0
            else abcd/totalcount
        end AS 'd',
        case totalcount
            when 0 then 0
            else defg/totalcount
        end AS 'e'
    From 
        Table1

In this query, I have same case statement in select query…Can i make it into single select statement.

“totalcount” is some value… abcd and defg are two column values from table1. If totalcount is zero, i dont want to divide, else I wish to divide to get an average value of abcd and defg.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:57:07+00:00Added an answer on May 17, 2026 at 5:57 pm

    At first view, I would say no, since you seem to use the exact same condition leading to different results.

    Besides, this looks odd to me, why would you need two SELECT CASE for only one condition? This makes no sense.

    Could you be more specific or give a real-world example of what you’re trying to ask, with “real” data so that we might better answer your question?

    EDIT #1

    Given that:

    Yes…it’s two different fields….and the valud i need is calculated one…which is Value = a/b. But i need to check if b is zero or not

    I would still answer no, as if they are two different fields, and you want both results in your result set, then you will need to write two CASE statements, one for each of the fields you need to verify whether it is zero-valued or not. Keep in mind that one CASE statement is equivalent to one single column only. So, if you need to check for a zero value, you are required to check for both independently.

    By the way, sorry for my misunderstanding, that must be a language barrier, unfortunately. =) But my requirement for “real” data example holds, since this might light us all up with some other related solutions, we never know! =)

    EDIT #2

    Considering you have to check for a value of 0, I would perhaps rewrite my code as follows, for the sake of readability:

    select a, b, c
            , case when condition1 <> 0 then abcd / condition1 else 0 end as Result1
            , case when condition2 <> 0 then defg / condition2 else 0 end as Result2
        from Table1
    

    In my opinion, it is leaner and swifter in the code, and it is easier to understand what is the intention. But after all, the result would be the same! No offense made here! =)

    EDIT #3

    After having read your question edit:

    “totalcount” is some value… abcd and defg are two column values from table1. If totalcount is zero, i dont want to divide, else I wish to divide to get an average value of abcd and defg.

    I say, if it is the average that you’re after, why not just use the AVG function which will deal with it internaly, without having to care about zero values?

    select a, b, c
            , AVG(abcd) as Result1
            , AVG(defg) as Result2
        from Table1
    

    Plus, considering having no record, the average can only be 0!

    I don’t know about your database engine, but if it is Oracle or SQL Server, and I think DB2 as well, they support this function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: SELECT rowid FROM table1 ORDER BY RANDOM() LIMIT 1
I have the following query: select column_name, count(column_name) from table group by column_name having
I have the following query: SELECT c.* FROM companies AS c JOIN users AS
I have the following SQL query: select expr1, operator, expr2, count(*) as c from
I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN
I have the following (shortened query): SELECT `Statistics`.`StatisticID`, COUNT(DISTINCT `Flags`.`FlagType`) AS `FlagCount` FROM `Statistics`
I have a following query (simplified): SELECT Id FROM dbo.Entity WHERE 1 = ALL
I have the following query: SELECT a.topicID, d.catalogFileID, d.catalogFileExtension, a.sortorder FROM catalog_topics a LEFT
I have the following query: SELECT dev.DeviceName, Count(dom.DomainID) AS CountOfDomains FROM tblDevices dev JOIN
Suppose I have the following query: select * from A, B, C, D where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.