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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:48:58+00:00 2026-06-17T09:48:58+00:00

In the Subquery, I would like to be able to add in the WHERE

  • 0

In the Subquery, I would like to be able to add in the WHERE clause:

duedate<=quotehed.duedate  

quotehed.duedate is in the main query. I don’t know how to bring the quotehed.duedate into the subquery because it is not in the same table as my subquery. I also don’t know how to make sure the quotehed.duedate will pull in the due date from the quote number that is selected from the parameter @p_quotenum.

My enitre query follows below. I greatly appreciate any help! Thanks!

SELECT partwhse.warehousecode,
       partwhse.allocqty,
       partwhse.onhandqty,
       quotehed.quotenum,
       quotehed.custnum,
       quotehed.datequoted,
       quotehed.duedate,
       quotedtl.quoteline,
       quotedtl.partnum,
       quotedtl.reqshipdate,
       quotedtl.sellingexpectedqty,
       plantwhse.plant,
       part.partdescription,
       t_partdtl1.totaldemand
FROM   part
       INNER JOIN quotedtl
         ON part.company = quotedtl.company
            AND part.partnum = quotedtl.partnum
       LEFT OUTER JOIN (SELECT company,
                               partnum,
                               requirementflag,
                               SUM(quantity) AS totaldemand,
                               plant,
                               duedate
                        FROM   partdtl AS partdtl_1
                        WHERE  ( company = 'lot' )
                               AND ( requirementflag = '1' )
                               AND ( plant = @p_plant )
                        GROUP  BY company,
                                  partnum,
                                  requirementflag,
                                  plant,
                                  duedate) AS t_partdtl1
         ON quotedtl.company = t_partdtl1.company
            AND quotedtl.partnum = t_partdtl1.partnum
       LEFT OUTER JOIN partwhse
                       INNER JOIN plantwhse
                         ON partwhse.company = plantwhse.company
                            AND partwhse.partnum = plantwhse.partnum
                            AND partwhse.warehousecode = plantwhse.warehousecode
         ON quotedtl.company = plantwhse.company
            AND quotedtl.partnum = plantwhse.partnum
       RIGHT OUTER JOIN quotehed
         ON quotedtl.company = quotehed.company
            AND quotedtl.quotenum = quotehed.quotenum
WHERE  ( quotehed.quotenum = @p_quotenum )
       AND ( quotehed.company = 'lot' )
       AND ( plantwhse.plant = @p_plant )
ORDER  BY quotehed.quotenum,
          quotedtl.quoteline 
  • 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-17T09:49:00+00:00Added an answer on June 17, 2026 at 9:49 am

    You should try to reorder your joins to place the subquery after you join quotehed. Once that is done then join the subquery to the quotehed table using the date filter, similar to this:

    SELECT partwhse.warehousecode, 
        partwhse.allocqty, 
        partwhse.onhandqty, 
        quotehed.quotenum, 
        quotehed.custnum, 
        quotehed.datequoted, 
        quotehed.duedate, 
        quotedtl.quoteline, 
        quotedtl.partnum, 
        quotedtl.reqshipdate, 
        quotedtl.sellingexpectedqty, 
        plantwhse.plant, 
        part.partdescription, 
        t_partdtl1.totaldemand
    FROM part
    INNER JOIN quotedtl
        ON part.company = quotedtl.company 
        AND part.partnum = quotedtl.partnum 
    LEFT JOIN plantwhse
        ON quotedtl.company = plantwhse.company 
        AND quotedtl.partnum = plantwhse.partnum 
    LEFT OUTER JOIN partwhse
        ON partwhse.company = plantwhse.company 
        AND partwhse.partnum = plantwhse.partnum 
        AND partwhse.warehousecode = plantwhse.warehousecode 
    RIGHT OUTER JOIN quotehed
        ON quotedtl.company = quotehed.company 
        AND quotedtl.quotenum = quotehed.quotenum
    LEFT OUTER JOIN
    (
        SELECT company, partnum, requirementflag, SUM(quantity) AS totaldemand, plant, duedate
        FROM partdtl AS partdtl_1
        WHERE (company = 'lot') 
            AND (requirementflag = '1') 
            and (plant=@p_plant)
        GROUP BY company, partnum, requirementflag, plant, duedate
    ) AS t_partdtl1 
        ON quotedtl.company = t_partdtl1.company 
        AND quotedtl.partnum = t_partdtl1.partnum 
        AND t_partdtl1.duedate < quotehed.duedate
    WHERE (quotehed.quotenum = @p_quotenum) 
        AND (quotehed.company = 'lot') 
        AND (plantwhse.plant = @p_plant)
    ORDER BY quotehed.quotenum, quotedtl.quoteline
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if it is possible to have subquery in a
In the following query, I would like the remove the subquery from the JOIN
I am writing a SQL query that I would like to add a column
I would like to execute some kind of subquery with my fetchedresultscontroller. I've got
I would like to know if it's possible to order by a comma-delimited set
I have a grouped query, and would like to filter it based on count(*)
I would like to run a query from a table where the content is
I have a query where I use a sub query, and I would like
I have run into something I don't know how to do with active record
Assuming I'm using a model like this How would I be able to find

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.