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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:31:03+00:00 2026-05-27T14:31:03+00:00

Is there a way to combine these queries so that I get a distinct

  • 0

Is there a way to combine these queries so that I get a distinct name, then the quantity for today, tomorrow, and the day after tomorrow

select name, SUM(qty) as qty_today, 0 as qty_tomororow, 0 as qty_om  
from order_item
where delivery_date = '2011-11-22'
group by name
union all
select name, qty as qty_today, SUM(qty) as qty_tomororow, 0 as qty_om  
from order_item
where delivery_date = '2011-11-23'
group by name
union all
select name, 0 as qty_today, 0 as qty_tomororow, SUM(qty) as qty_om  
from order_item
where delivery_date = '2011-11-24'
group by name

UPDATE
Given the table
name, qty, delivery_date
Chicago, 1, 2011-11-22
New York, 2, 2011-11-22
Chicago, 3, 2011-11-23

I would expect
Chicago, 1, 3, 0
New York, 1, 0, 0

  • 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-27T14:31:04+00:00Added an answer on May 27, 2026 at 2:31 pm

    If you are trying to combine all of them in one dataset you can try this,

    SELECT name, 
           SUM(CASE WHEN delivery_date = '2011-11-22' THEN qty 
                    ELSE 0 END) AS qty_today,
           SUM(CASE WHEN delivery_date = '2011-11-23' THEN qty 
                    ELSE 0 END) AS qty_tomororow,
           SUM(CASE WHEN delivery_date = '2011-11-24' THEN qty 
                    ELSE 0 END) AS qty_om
    from order_item
    where delivery_date between '2011-11-22' and '2011-11-24'
    group by name
    

    This would work in TSQL (MS SQL), hopefully it’s the same syntax in MySql

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

Sidebar

Related Questions

Is there a way to combine 2 vectors in MATLAB such that: mat =
is there way how to get name ov event from Lambda expression like with
I am trying to combine these 2 queries in such a way to determine
Is there any way to combine all of this to reduce the amount of
Is there a way to combine static tableview cells (static content) with dynamic tableview
Is there any way I can combine list of expressions into one? I have
Is there a built-in php function, or a simple (efficient!) way to combine built-in
Is there an easy way using grep (or combine with other standard command line
trying to save a step and combine these two queries but not sure how
I have to combine these two mySQL queries into one. I duplicated a solution

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.