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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:26:08+00:00 2026-05-31T12:26:08+00:00

I have a table A with the following columns: Status varchar StateCode bit OpportunityID

  • 0

I have a table A with the following columns:

Status varchar
StateCode bit
OpportunityID int

so if i do: SELECT count(distinct opportunityID) from table A group by Status I get the count. Now in the same query I want to add another columns called CurrentCount and HistoricalCount. The current count is the count of distinct opportunityID for a particular Status and StateCode = 0 and the HistoricalCount is the count of distinct opportunityID for a particular Status and StateCode = 1. In other words Count = (CurrentCount + HistoricalCount ).

How do I accomplish this ??

Any ideas and suggestions are well appreciated !

  • 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-31T12:26:10+00:00Added an answer on May 31, 2026 at 12:26 pm

    You can use a case in your count statements so you only count where StateCode has the desired value.

    select Status,
           count(distinct OpportunityID) as [Count],
           count(distinct case when StateCode = 1 then OpportunityID end) as CurrentCount,
           count(distinct case when StateCode = 0 then OpportunityID end) as HistoricalCount
    from YourTable
    group by Status
    

    In other words Count = (CurrentCount + HistoricalCount ).

    No, that will not be true if you have a OpportunityID that has both StateCode as 1 and 0.

    Ex:

    declare @T table
    (
      Status varchar(10),
      StateCode bit,
      OpportunityID int
    )
    
    insert into @T values
    ('Status1', 1, 1),
    ('Status1', 1, 2),
    ('Status1', 0, 2),
    ('Status2', 0, 1),
    ('Status2', 0, 2)
    
    select Status,
            count(distinct OpportunityID) as [Count],
            count(distinct case when StateCode = 1 then OpportunityID end) as CurrentCount,
            count(distinct case when StateCode = 0 then OpportunityID end) as HistoricalCount
    from @T
    group by Status
    

    Result:

    Status     Count       CurrentCount HistoricalCount
    ---------- ----------- ------------ ---------------
    Status1    2           2            1
    Status2    2           0            2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table [Users] with the following columns: INT SmallDateTime Bit Bit [UserId],
I have a table called tblRentalRates with the following columns: rate_id (int) rate_startdate (datetime)
I have a pictures table that has the following columns: PICTURE_ID int IDENTITY(1000,1) NOT
I have a Product table with non-null quantity (decimal) and status (int) columns, and
Suppose I have a table with 2 columns (status and date) like the following:
I have a table with the following columns: A B C --------- 1 10
I have a table with the following columns: EntityId, EntityName, EntityProfile, ................. I want
Lets say I have a table with the following columns: Qty, INTEGER SaleDate, DATETIME
I have a table named with Sales having the following columns: Sales_ID|Product_Code|Zone|District|State|Distributor|Total_Sales Now i
I have a Name table with the columns NameID Name TypeID With the following

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.