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

  • Home
  • SEARCH
  • 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 8590701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:21:16+00:00 2026-06-11T23:21:16+00:00

I have a data in Microsoft SQL server with the following format: id1 id2

  • 0

I have a data in Microsoft SQL server with the following format:

id1 id2 month   quantA  quantB  
1   10  1   5   15  
1   10  1   10  20  
1   10  2   5   10  
1   10  2   10  NULL  
1   11  1   NULL    NULL  
1   11  2   5   NULL  
1   11  2   10  5  
2   10  1   10  20  
2   10  1   5   NULL  
2   11  2   NULL    NULL  

I need to construct a table grouped by id1 and month with the following columns:

id1  
month  
var1 = count how many *distinct* id2 by month and id1 for which quantA!=Null  
var2 = count how many *distinct* id2 by month and id1 for which quantB!=Null
  • 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-11T23:21:17+00:00Added an answer on June 11, 2026 at 11:21 pm

    You can construct the query basically how you have written it:

    select id1, month,
           count(distinct case when quantA is not null then id2 end) as var1,
           count(distinct case when quantB is not null then id2 end) as var2
    from t
    group by id1, month
    

    COUNT DISTINCT ignores NULLs when doing the count.

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

Sidebar

Related Questions

I have data in Microsoft SQL Server 2008 that I need to SUM. The
I have a Microsoft SQL server DB that imports some data which needs a
I have a Microsoft SQL Server 2008 query that returns data from three tables
I need to replicate data from Microsoft SQL Server to MySQL or PostgreSQL. The
I have created a database emailDatabase , its stored in C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA
We have two stored procedures in Microsoft SQL Server that return data that is
I have the following table join that runs fine in Microsoft SQL Server and
I have the following software installed on the machine: Microsoft SQL Server Express 2005
I have a database called MyDB in a Microsoft SQL Server 2008 Express instance
I have a Access Data Project that connects to an SQL server database. Recently

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.