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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:39:06+00:00 2026-06-17T08:39:06+00:00

I will simplfy this: I have two SQL expressions which works OK: First: select

  • 0

I will simplfy this:

I have two SQL expressions which works OK:

First:

select count(*) as number1
 from T1
where DATE1>'2012-01-01' and DATE2<'2012-12-31'

Result:13

select  count(*) as number2 
from T1
where DATE3>DATE2 and CURDATE()>DATE2

Result:5

But when I try to insert those two COUNTS in GROUP BY I am getting as resulyt always 13!!!

SELECT NAME,
 COUNT(case when DATE1>'2012-01-01' and DATE2<'2012-12-31' then 1 else 0 end) as number1, 
COUNT (case when (DATE3>DATE2 and CURDATE()>DATE2) then 1 else 0 end) as number 2
from T1

I am getting as result:

NAME  NUMBER1  NUMBER2
A       5         5
B       4         4
C       4         4

But I should get:

NAME  NUMBER1  NUMBER2
A       5         4
B       4         0
C       4         1

So that SUM of columns be 13 and 5 like in first two queries . What am I doing wrong?
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-17T08:39:07+00:00Added an answer on June 17, 2026 at 8:39 am

    COUNT(expression) counts not null expressions. You can modify your query by changing the ELSE 0 to ELSE NULL or by removing it (the ELSE NULL is implied at CASE expressions):

    SELECT name,
        COUNT(CASE WHEN date1 > '2012-01-01' AND date2 < '2012-12-31' 
                  THEN 1 END
             ) AS number1, 
        COUNT(CASE WHEN date3 > date2 AND CURDATE() > date2 
                  THEN 1 END
             ) AS number2
    FROM T1 
    GROUP BY name ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two applications communicating via RMI, a slave server (of which there will
I have a windows form simply like this: 1) a button when clicked will
This problem is giving me a real headache. I have two tables in my
On a non-production machine, I have two instances of SQL Server 2005. I want
I'm writing an application that will have a SQL Server backend that will store
I need to write a stored procedure that will provide the data from two
Right now I have two repeaters: 1 will be shown when the page loads,
I have two tables tableA and tableB, which have the same structure: rowid: big
I have a working query that will return some results(records) from my database, like:
I have a workbook with two sheets. The first is full of data and

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.