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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:30:51+00:00 2026-05-24T16:30:51+00:00

See the SQL query below, it count the number of Yes and No between

  • 0

See the SQL query below, it count the number of Yes and No between the CheckDate

On the MatchSales field – it do not work how I wanted to be. It should find D.MobileNO (depending on the CheckDate) match to S.mobile (Check all record in the sales table)

SELECT D.Username, 
    SUM(CASE WHEN D.type = 'Yes' AND UNIX_TIMESTAMP(CheckDate) >= $From_timestamp AND UNIX_TIMESTAMP(CheckDate) <= $To_timestamp THEN 1 ELSE 0 END) as Yes, 
    SUM(CASE WHEN D.type = 'No' AND UNIX_TIMESTAMP(CheckDate) >= $From_timestamp AND UNIX_TIMESTAMP(CheckDate) <= $To_timestamp THEN 1 ELSE 0 END) as No, 
    SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as MatchSales
FROM dairy as D 
   LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile
WHERE source = 'Company' 
GROUP BY D.Username
  • 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-24T16:30:52+00:00Added an answer on May 24, 2026 at 4:30 pm

    simply add the Checkdate condition from your CASE to the where clause:

    SELECT D.Username,      
          SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) AS Yes,      
          SUM(CASE WHEN D.type = 'No'  THEN 1 ELSE 0 END) AS No,      
          SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) AS MatchSales 
     FROM dairy AS D     
     LEFT JOIN (SELECT DISTINCT mobile FROM sales) AS S ON D.MobileNo = S.mobile 
     WHERE D.source = 'Company'  
     AND   UNIX_TIMESTAMP(D.CheckDate) >= $From_timestamp 
     AND   UNIX_TIMTIMESTAMP(D.CheckDate) <= $To_timestamp 
     GROUP BY D.Username
    

    without it you will full scan dairy

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

Sidebar

Related Questions

See the SQL query below, it count the total records that match with numbers
See the SQL Query below, it seem to be working well. It allow me
See the SQL query below - it allow you to search the Shop by
How to see this format hh:mm from hh:mm:ss in SQL Server 2008 query ?
see also Differences between LINQ to Objects and LINQ to SQL queries We are
I have a SQL Query with a join between several tables. When I run
I have a SQL Query as below: SELECT urban_appr, urban_in, rural_appr, rural_in, ground_appr, ground_in
I have written an SQL Query that brings the result in below format ResourceName
See below 2 versions of code and SQL it produces. I want to load
The query below calculates the percentage of a product between FY2009 vs. FY2010 for

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.