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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:19:14+00:00 2026-06-14T07:19:14+00:00

Hi I would like to write one SQL statement that returns both the count

  • 0

Hi I would like to write one SQL statement that returns both the count and count(distinct) from the same table but with different WHERE clause.

e.g.

People
-----------------------
ID      Name        Sex
1       Alex        F
2       Alex        M
3       Alex        M
4       Andy        F
5       Andy        M
6       Ann         F
7       Ann         F

In this example, I can write SELECT (SELECT COUNT(1) FROM People), (SELECT COUNT(DISTINCT Name) FROM People), but thats just too many SELECT. So I write SELECT COUNT(1), COUNT(DISTINCT Name) FROM People instead.

But what if I would like to return something like SELECT (SELECT COUNT(1) FROM People), (SELECT COUNT(DISTINCT Name) FROM People WHERE Sex='M'). How do you do the same like above to have less SELECT?

I don’t know if this is related, I know that I can write something like SELECT COUNT(1), SUM(CASE WHEN Sex='M' THEN 1 ELSE 0 END) FROM People to have different WHERE clauses in one SELECT, but thats only for COUNT, it doesn’t handle DISTINCT. How do you use this when DISTINCT is needed?

The DB is SQL Server 2008 R2, Thank you

  • 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-14T07:19:15+00:00Added an answer on June 14, 2026 at 7:19 am

    Try this:

    Select count(id), count(distinct case when sex = 'M' then name else null end)
    From people
    

    You can see it in SQL fiddle here:

    • SQL Fiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write an if statement that would continue to repeat a
I would like to write a query that returns a single date value, that
I would like to write an if statement that will do something base on
I would like to write a single SQL command to drop multiple columns from
How do I write a SQL statement that would arrange a column of serial
I would like to write to functional test for one of my rails 3
I would like to write a function that creates a plot of a set
I would like to write a League Fixture generator in python, but I can't.
I would like to write a cross-platform function in C++ that contains system calls.
I want to write a query that will match up a string from one

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.