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 9254293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:25:53+00:00 2026-06-18T11:25:53+00:00

I was just wondering if there is any way to get two separate count

  • 0

I was just wondering if there is any way to get two separate “count” totals from a table using one query? That is, using a table similar to the following I would like to retrieve each code (distinct) and show the total number of status’ NOT equal to X or D, and then have an additional column that shows the total number of status’ equal to X or D and the cancel date is greater than a given date (say, the last 14 days).

Table:

Code:  Status  Cancel_Date
-----------------------------------
AAA    X       2012-02-01
AAA
BBB    X       2012-02-01
AAA    D       2012-01-01
AAA
BBB    
BBB    D       2012-02-01
BBB    X       2012-01-01

Example result (based on the above data):

Code:  TotalNotXorD     TotalXorD
------------------------------------
AAA    2                1
BBB    1                2

TotalNotXorD: e.g.

select code, count(*) 
from table 
where status not in('X','D') 
group by code

TotalXorD: e.g.

select code, count(*) 
from table 
where status in('X','D') 
  and cancel_date >= '2012-02-01' 
group by code

I have looked at doing subqueries etc. but I can’t seem to get the results I need.

Any ideas?

Thanks.

  • 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-06-18T11:25:54+00:00Added an answer on June 18, 2026 at 11:25 am

    SELECT  a.code,
            COALESCE(b.totalNotXorD, 0 ) totalNotXorD,
            COALESCE(c.totalXorD, 0 ) totalXorD,
    FROM    (SELECT DISTINCT Code FROM tableName) a
            LEFT JOIN
            (
                select code, count(*) totalNotXorD
                from table 
                where status not in('X','D') 
                group by code
            ) b ON a.code = b.code
            LEFT JOIN
            (
                select code, count(*) totalXorD
                from table 
                where status in('X','D') 
                  and cancel_date >= '2012-02-01' 
                group by code
            ) c ON a.code = c.code
    

    or simply doing CASE

    SELECT  Code,
            SUM(CASE WHEN status NOT IN ('X','D') OR status IS NULL THEN 1 ELSE 0 END) TotalNotXorD,
            SUM(CASE WHEN status IN ('X','D') AND cancel_date >= '2012-02-01' THEN 1 ELSE 0 END) TotalXorD  
    FROM    tableName
    GROUP   BY Code
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if there is any way to get the NS records in C#.
Just wondering if there is any way (in C) to get the contents of
Just wondering is there any way I can check whether the url links to
I'm just wondering, is there any way to run Selenium tests on Android in
I was wondering if there was any decent way, other than NSLog-ing just about
Just wondering if there are any pitfalls using .net as the extension for a
I was wondering if is there any way to get the pressure level (pressure
i just wondering is there any performance issue or anything thing wrong if that
Just wondering if there is any benchmark software that I can download that will
Was just wondering if there are any built in functions in c++ OR c#

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.