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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:04:50+00:00 2026-06-17T21:04:50+00:00

I’m trying to join four separate queries on PROD_CD to return the correct output

  • 0

I’m trying to join four separate queries on “PROD_CD” to return the correct output into one query to prevent having to merge the queries together in another language after. With the current one (and I’ve tried many variations, all with various problems), I’m receiving a lot of duplicate results and different numbers of duplicates for each.

Here’s the current query I’ve been trying (all the date functions are for determining dataset over a span of time – the database is very old and uses Clarion time):

$query_ats = "SELECT 
            plog.prod_cd as prod_id,
            ord_log.ORDER_QTY as total_so,
            ediordlg.ORDER_QTY as total_edi_so,
            inv_data.IN_STOCK as in_stock
        FROM plog 
        INNER JOIN ord_log 
            ON plog.prod_cd = ord_log.prod_cd 
        INNER JOIN ediordlg 
            ON plog.prod_cd = ediordlg.prod_cd AND ord_log.prod_cd = ediordlg.prod_cd
        INNER JOIN inv_data 
            ON plog.prod_cd = inv_data.prod_cd AND ord_log.prod_cd = inv_data.prod_cd AND ediordlg.prod_cd = inv_data.prod_cd
        WHERE 
            inv_data.CLASS_CD = 'ALG7' 
        AND 
            dateadd(day, plog.EST_DT, '18001228') BETWEEN getdate() and dateadd(day, $x, getdate())
        AND 
            dateadd(day, ord_log.SHIP_DT, '18001228') BETWEEN getdate() and dateadd(day, $x, getdate())
        AND 
            dateadd(day, ediordlg.SHIP_DT, '18001228') BETWEEN getdate() and dateadd(day, $x, getdate())
        GROUP BY plog.prod_cd, plog.log_qty, ord_log.ORDER_QTY, ediordlg.ORDER_QTY, inv_data.IN_STOCK
        ORDER BY plog.prod_cd ASC";

And this is a sample of what it outputs:

Array
(
[prod_id] => ALG-809
[total_so] => 4
[total_edi_so] => 46
[in_stock] => 0
)
Array
(
[prod_id] => ALG-809
[total_so] => 6
[total_edi_so] => 46
[in_stock] => 0
)
Array
(
[prod_id] => ALG-809
[total_so] => 7
[total_edi_so] => 46
[in_stock] => 0
)

Here are the four separate queries that return the correct results:

$query_stock = "SELECT 
                prod_cd, 
                inv_data.DESCRIP,
                inv_data.IN_STOCK
            from 
                inv_data 
            where 
                inv_data.CLASS_CD = 'ALG7'
            ORDER BY
                inv_data.prod_cd ASC";

$query_po = "SELECT 
            plog.prod_cd, 
            SUM(plog.log_qty) as total_po
        FROM 
            plog JOIN inv_data ON plog.prod_cd = inv_data.prod_cd 
        WHERE 
            inv_data.CLASS_CD = 'ALG7'
        AND 
            dateadd(day, EST_DT, '18001228') BETWEEN getdate() and dateadd(day, $x, getdate())
        GROUP BY 
            plog.prod_cd
        ORDER BY
            plog.prod_cd ASC";

$query_so = "SELECT 
            ord_log.prod_cd,
            SUM(ord_log.ORDER_QTY) as total_so
        FROM 
            ord_log JOIN inv_data ON ord_log.prod_cd = inv_data.prod_cd 
        WHERE 
            inv_data.CLASS_CD = 'ALG7' 
        AND 
            dateadd(day, SHIP_DT, '18001228') BETWEEN getdate() and dateadd(day, $x, getdate()) 
        GROUP BY 
            ord_log.PROD_CD
        ORDER BY
            ord_log.prod_cd ASC";

$query_edi = "SELECT 
            ediordlg.prod_cd,
            SUM(ediordlg.ORDER_QTY) as total_so_EDI
        FROM
            ediordlg JOIN inv_data ON ediordlg.prod_cd = inv_data.prod_cd 
        WHERE 
            inv_data.CLASS_CD = 'ALG7' 
        AND 
            dateadd(day, SHIP_DT, '18001228') BETWEEN getdate() and dateadd(day, $x, getdate()) 
        GROUP BY 
            ediordlg.PROD_CD
        ORDER BY
            ediordlg.prod_cd ASC";

I’m sure it’s the JOIN I’m using but I can’t figure it out for the life of me. Any suggestions? Thanks!

  • 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-17T21:04:51+00:00Added an answer on June 17, 2026 at 9:04 pm

    Rule 1 of SQL in cases like this: Select initially from the thing that you want rows out of, join to the things you want more information about. In your case what you want seems to be the product ID and this seems to be uniquely stored in the inv_data table, so we’ll start with that.

    select
            i.prod_cd as product,
            i.descrip as description,
            i.in_stock
        from
            inv_data as i
        where
            i.class_cd = 'ALG7'
        order by
            i.prod_cd asc
    ;
    

    You’ll get one of each because only one of each is stored. The rest is just details. Let’s add some joins

    select
            i.prod_cd as product,
            i.descrip as description,
            i.in_stock,
            sum(l.order_qty) as l_total_so,
            sum(e.order_qty) as e_total_so
        from
            inv_data as i
            inner join plog as p
                on i.prod_cd = p.prod_cd
            inner join ord_log as l
                on i.prod_cd = l.prod_cd
            inner join ediordlg as e
                on i.prod_cd = e.prod_cd
        where
            i.class_cd = 'ALG7'
        order by
            i.prod_cd asc
        group by
            i.prod_cd,
            i.descrip,
            i.in_stock
    ;
    

    You don’t need to list so many columns in your on clauses because they’re all the same anyway (by definition, because the earlier inner join succeeded).

    If it turns out that plog and not inv_data is your master table simply reverse them in the query. If you want both total values together, use sum(l.order_qty + e.order_qty) as total_so instead of creating two columns.

    The thing to understand is that joins can multiply the results. Understanding which tables have more and doing something in each case to limit the “extra” results each time they would be extra will result in a clean resultset. In this case probably just summing and grouping is enough, but in a complex case you would need to join to a sub-query that selects back a sufficiently distinct set.

    And, on a related note, distinct is poison for your query! It has a very specific purpose which is not to fix “too many duplicate rows after joining.” If you’re using it for that you probably have a bug that could have unknown other side effects. Try to fix it with group by and smarter on statements first, then nested queries.

    Not related to your question, but important: It appears that you are expanding a variable $x inside your query. This is probably not safe (or efficient); try to use a parametrized query instead.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.