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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:07:35+00:00 2026-06-12T18:07:35+00:00

I am trying to do all of these SELECT statements in all one query

  • 0

I am trying to do all of these SELECT statements in all one query so I will be able to further it and group it. I believe I have to tell it to JOIN on TABLE1. I can tell you that it should be JOINing on the field called ITEM. I have tried dozens of JOIN statements none of which does the trick because I have two WHERE statements in my subqueries.

SELECT ITEM, DSI, LEADTIME,
(SELECT COUNT(ORDER_NUMBER) FROM SUBTABLE1 TR1 WHERE TRANS_DATE BETWEEN DATE_SUB(curdate(), INTERVAL 730 DAY) AND DATE_SUB(curdate(), INTERVAL 365 DAY))
as OLDORDERS,
(SELECT COUNT(ORDER_NUMBER) FROM SUBTABLE2 TR2 WHERE TRANS_DATE BETWEEN DATE_SUB(curdate(), INTERVAL 364 DAY) AND curdate())
as NEWORDERS
FROM TABLE1

Displays:

ITEM   |  DSI  |  LEADTIME  | OLDORDERS | NEWORDERS
PROD-1    0         1          16036      38399
PROD-2    1         0          16036      38399
PROD-3    1         1          16036      38399

Again…I believe I need it to JOIN the field ITEM on the subqueries, but I do not know how to do this, any ideas?

  • 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-12T18:07:36+00:00Added an answer on June 12, 2026 at 6:07 pm

    You don’t actually need a JOIN, per se; rather, you need to “correlate” your subqueries, so that they refer to data in their containing query.

    You haven’t given your exact table definitions, so I can’t say for sure, but here’s my guess at what you need:

    SELECT item, dsi, leadtime,
           ( SELECT COUNT(order_number)
               FROM subtable1
              WHERE trans_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 730 DAY)
                                   AND DATE_SUB(CURDATE(), INTERVAL 365 DAY)
                    -- restrict to "current" record from TABLE1:
                AND subtable1.item = table1.item
           ) as OLDORDERS,
           ( SELECT COUNT(order_number)
               FROM subtable1
              WHERE trans_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 364 DAY)
                                   AND CURDATE()
                    -- restrict to "current" record from table1:
                AND subtable1.item = table1.item
           ) as NEWORDERS
      FROM table1
    ;
    

    That’s assuming that table1.item is the primary key, and that subtable1.item is a foreign-key referring to it. Naturally you’ll have to adjust the query if that’s not the case.

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

Sidebar

Related Questions

I have been trying to compute the total sum of all these ord s
I'm trying to find and collect all the elements that meet these requirements: var
I have been trying all afternoon to get the jQuery Sifr Plugin ( http://jquery.thewikies.com/sifr/
I have been trying all day to get some data properly formatted in a
I have very long select query which i need to filter based on some
Trying to build a somewhat complicated WordPress query. In one query, I'm trying to:
. I have 4 tables as shown above. Now I'm trying to get all
I am trying to output all of my database reports into one report. I'm
Let's suppose I have three different select statements, like this SELECT 'Final', ID FROM
I have two tables, one for articles/polls, and one for the votes on these

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.