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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:59:19+00:00 2026-06-14T01:59:19+00:00

I am sure there is some easy fix to this. I just have basically

  • 0

I am sure there is some easy fix to this. I just have basically no experience with writing SQL statements and so I don’t know all of the commands, tricks or syntax to get what I need.

I have the following two SQL SELECT statements I need to combine:

1:

SELECT 
    h.jhhold, h.jhjob, h.jhrev, o.jadesc, o.jaoqty, o.jacqty, o.japo, o.javend  
FROM 
    Jhead AS h  
LEFT JOIN 
    jjops AS o ON h.jhjob = o.jajob  
WHERE 
    h.jhpcmp = 0 
    AND o.jatype = 2 
    AND o.jacqty < o.jaoqty 
    AND o.japcmp = 0 
    AND o.japo = 0  

2:

SELECT 
    h.jhhold, h.jhjob, h.jhrev, o.jadesc, o.jaoqty, o.jacqty, o.japo, o.javend, 
    p.hdprcd, p.hdrecd, p.hdseq  
FROM 
    Jhead AS h 
LEFT JOIN 
    jjops AS o ON h.jhjob = o.jajob   
LEFT JOIN 
    hpodt AS p ON h.jhjob = p.hdjob   
WHERE 
    h.jhpcmp = 0 
    AND o.jatype = 2 
    AND o.jacqty < o.jaoqty 
    AND o.japcmp = 0 
    AND p.hdjob = h.jhjob 
    AND p.hdpo = o.japo 
    AND p.hdseq = o.jaseq

Both of these two statements return the results I need. The first statement basically returns results where o.japo = 0 (it is never null). The second returns a result where o.japo is not zero and then I attempt to join another table to the data. I did try stick a UNION ALL in between but all union examples I looked up did not have unqiue WHERE statement per select statement.

.
I had initially attempted the following statement (below) but was getting multiple lines of data when o.japo = 0 and I could not figure out why it was picking up the extra lines but I suspect it was the second left join joining incorrect/unwanted data. When o.japo = 0 all of the hpodt (p.) fields should have been null but instead were populated with data from somewhere.

SELECT h.jhhold, h.jhjob, h.jhrev, o.jadesc, o.jaoqty, o.jacqty, o.japo, o.javend, p.hdprcd, p.hdrecd, p.hdseq 
FROM Jhead AS h 
LEFT JOIN jjops AS o ON h.jhjob = o.jajob 
LEFT JOIN hpodt AS p ON h.jhjob = p.hdjob 
WHERE (h.jhpcmp = 0 AND o.jatype = 2 AND o.jacqty < o.jaoqty AND o.japcmp = 0 AND p.hdjob = h.jhjob AND o.japo != 0 AND p.hdpo = o.japo AND p.hdseq = o.jaseq AND o.jaopr != 'PT') OR (h.jhpcmp = 0 AND o.jatype = 2 AND o.jacqty < o.jaoqty AND o.japcmp = 0 AND o.japo = 0 AND o.jaopr != 'PT')
ORDER BY h.jhjob, o.jaseq, p.hdrecd
  • 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-14T01:59:20+00:00Added an answer on June 14, 2026 at 1:59 am

    Put a UNION between the two statements.

    UNION needs to have the same number of columns, so you’ll need to do a little extra work.

    Add a few nulls to the first select statement to cover the columns that don’t exist.

    SELECT 
       -- Common to both tables
       h.jhhold, h.jhjob, h.jhrev, 
       o.jadesc, o.jaoqty, o.jacqty, 
       o.japo, o.javend, 
       -- These columns don't exist here.
       -- include them anyway as NULL
       NULL as hdprcd,
       NULL AS hdrecd, 
       NULL AS hdseq  
    FROM Jhead AS h  
    LEFT JOIN 
      jjops AS o ON h.jhjob = o.jajob  
    WHERE 
        h.jhpcmp = 0 
    AND o.jatype = 2 
    AND o.jacqty < o.jaoqty 
    AND o.japcmp = 0 
    AND o.japo = 0
    -- The big boy, combining the results of two queries
    ---
    UNION
    SELECT 
       h.jhhold, h.jhjob, h.jhrev, 
       o.jadesc, o.jaoqty, o.jacqty, 
       o.japo, o.javend, p.hdprcd, 
       p.hdrecd, p.hdseq  
    FROM Jhead AS h 
    LEFT JOIN 
       jjops AS o 
    ON h.jhjob = o.jajob   
    LEFT JOIN 
       hpodt AS p 
    ON h.jhjob = p.hdjob   
    WHERE 
        h.jhpcmp = 0 
    AND o.jatype = 2 
    AND o.jacqty < o.jaoqty 
    AND o.japcmp = 0 
    AND p.hdjob = h.jhjob 
    AND p.hdpo = o.japo 
    AND p.hdseq = o.jaseq
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure there is some really easy solution to this problem, at least it
I'm pretty sure there's some quick and easy error in this code but somehow
Pretty sure there is an easy answer to this, but just can't find the
I'm sure this is an easy one for someone out there. I have a
I'm sure there's some way to do this with the \defgroup, \addgroup and \@{
Am sure there's answer on Google but I don't know how to formulate my
Im sure there is a very simple solution for this. I have a bunch
I'm sure there's a really easy way of doing this. I'm trying to take
Im sure there is an easy answer for this one, but I've been looking
(this is an easy and silly question but I don't know how to do)

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.