# Union 2 BAQ sub-queries, and join the result of that union to another table

**URL:** <https://www.epiusers.help/t/union-2-baq-sub-queries-and-join-the-result-of-that-union-to-another-table/46934>\
**Category:** Epicor ERP 10\
**Created:** [August 15, 2018, 8:01pm UTC](https://www.epiusers.help/t/union-2-baq-sub-queries-and-join-the-result-of-that-union-to-another-table/46934 "2018-08-15T20:01:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ckrusen](https://www.epiusers.help/user_avatar/www.epiusers.help/ckrusen/32/50584_2.png) [@ckrusen](https://www.epiusers.help/u/ckrusen)\
**Post date:** [August 15, 2018, 8:01pm UTC](https://www.epiusers.help/t/union-2-baq-sub-queries-and-join-the-result-of-that-union-to-another-table/46934/1 "2018-08-15T20:01:04Z")

</div>

Any way to make a union between 2 sub queries, and then join that result another table?

I want a union of Part and PartSub, where Part.PartNum = UserParam and PartSub.SubPart = UserParam.

```auto
Table: Part
PartNum    
RM-0001
RM-0002
RM-0003
XYZPQD
XYZXXX

Table: PartSubs
PartNum SubPart
XYZPQD RM-0001
XYZXXX RM-0003
XYZYYY RM-0003

Desired Results

UserParam = RM-0001
(2) Records
PartNum
RM-0001
XYZPQD

UserParam = RM-0002
(1) Record
PartNum
RM-0002

UserParam = RM-0003
(3) Records
PartNum
RM-0003
XYZXXX
XYZYYY

```

Then join those results to another table (PartTran)

---

<div class="post-metadata">

**Author:** ![Aaron\_Moreng](https://www.epiusers.help/user_avatar/www.epiusers.help/aaron_moreng/32/745_2.png) [@Aaron\_Moreng](https://www.epiusers.help/u/Aaron_Moreng)\
**Post date:** [August 15, 2018, 8:04pm UTC](https://www.epiusers.help/t/union-2-baq-sub-queries-and-join-the-result-of-that-union-to-another-table/46934/2 "2018-08-15T20:04:47Z")

</div>

I’ll try to conceptually help if I can.  
If you create the first query as a sub-query and the second query as a subquery, and assuming the two sub queries match columns, you can create a union query from those. On that union, you can create a subquery from it and then join the new unionized (hah) to whatever you want.

---

<div class="post-metadata">

**Author:** ![ckrusen](https://www.epiusers.help/user_avatar/www.epiusers.help/ckrusen/32/50584_2.png) [@ckrusen](https://www.epiusers.help/u/ckrusen)\
**Post date:** [August 15, 2018, 8:11pm UTC](https://www.epiusers.help/t/union-2-baq-sub-queries-and-join-the-result-of-that-union-to-another-table/46934/3 "2018-08-15T20:11:28Z")

</div>

First off … I hate that the top level query is still referred to as a “sub-query”.

So I’d have the following three sub-queries?

- SubQuery1 of type ‘TopLevel’
- SubQuery2 of type ‘InnerSubQuery’
- SubQuery3 of type ‘Union’

With SubQuery2 and SubQuery3 having the same columns (as required by unions)

And then only include and join SubQuery2 in the layout of SubQuery1?

How does it know to limit the union between the SQ’s 2 & 3?

---

<div class="post-metadata">

**Author:** ![Aaron\_Moreng](https://www.epiusers.help/user_avatar/www.epiusers.help/aaron_moreng/32/745_2.png) [@Aaron\_Moreng](https://www.epiusers.help/u/Aaron_Moreng)\
**Post date:** [August 15, 2018, 8:22pm UTC](https://www.epiusers.help/t/union-2-baq-sub-queries-and-join-the-result-of-that-union-to-another-table/46934/4 "2018-08-15T20:22:35Z")

</div>

If the union is then encapsulated inside another sub-query, it will behave as any other query would.  
I think you need more subs than 3, since you need to wrap the union up too.

---

<div class="post-metadata">

**Author:** ![ckrusen](https://www.epiusers.help/user_avatar/www.epiusers.help/ckrusen/32/50584_2.png) [@ckrusen](https://www.epiusers.help/u/ckrusen)\
**Post date:** [August 15, 2018, 8:34pm UTC](https://www.epiusers.help/t/union-2-baq-sub-queries-and-join-the-result-of-that-union-to-another-table/46934/5 "2018-08-15T20:34:03Z")

</div>

I got it working with 3 subqueries (counting the TopLevel one)

I needed to add parenthesis on the SubQueryList tab  
 ![image](https://epiusers.help/uploads/default/original/2X/8/8ba243b74d035214f42863130bfb0795942442fa.png)

I now get the desired output. I enter CB-0001 (CB-0001 is an Alternate for the now inactive XCHM#8)

 ![image](https://epiusers.help/uploads/default/original/2X/e/e84ece094501df16dfc3281d5c898c4a5e54565c.png)

---

<div class="post-metadata">

**Author:** ![Aaron\_Moreng](https://www.epiusers.help/user_avatar/www.epiusers.help/aaron_moreng/32/745_2.png) [@Aaron\_Moreng](https://www.epiusers.help/u/Aaron_Moreng)\
**Post date:** [August 15, 2018, 9:12pm UTC](https://www.epiusers.help/t/union-2-baq-sub-queries-and-join-the-result-of-that-union-to-another-table/46934/6 "2018-08-15T21:12:01Z")

</div>

Glad to hear it, good work!

---

<div class="post-metadata">

**Author:** ![timshuwy](https://www.epiusers.help/user_avatar/www.epiusers.help/timshuwy/32/90122_2.png) [@timshuwy](https://www.epiusers.help/u/timshuwy)\
**Post date:** [August 15, 2018, 9:12pm UTC](https://www.epiusers.help/t/union-2-baq-sub-queries-and-join-the-result-of-that-union-to-another-table/46934/7 "2018-08-15T21:12:53Z")

</div>

(((Hmmm… Why have I never seen those parentheses on this page before?)))  
Thanks for the tip!

---

<div class="post-metadata">

**Author:** ![Banderson](https://www.epiusers.help/user_avatar/www.epiusers.help/banderson/32/50875_2.png) [@Banderson](https://www.epiusers.help/u/Banderson)\
**Post date:** [August 15, 2018, 9:16pm UTC](https://www.epiusers.help/t/union-2-baq-sub-queries-and-join-the-result-of-that-union-to-another-table/46934/8 "2018-08-15T21:16:26Z")

</div>

> [@ckrusen](#):
>
> First off … I hate that the top level query is still referred to as a “sub-query”.

I always rename my subs so I know what they heck they are. My top level I rename to “TopLevel”

---

<div class="post-metadata">

**Author:** ![ckrusen](https://www.epiusers.help/user_avatar/www.epiusers.help/ckrusen/32/50584_2.png) [@ckrusen](https://www.epiusers.help/u/ckrusen)\
**Post date:** [August 16, 2018, 12:08am UTC](https://www.epiusers.help/t/union-2-baq-sub-queries-and-join-the-result-of-that-union-to-another-table/46934/9 "2018-08-16T00:08:03Z")

</div>

I name mine ‘Main’

I was just experimenting in the example above. I’ll name them properly when I make the full BAQ

---

<div class="post-metadata">

**Author:** ![fredmeissner](https://www.epiusers.help/user_avatar/www.epiusers.help/fredmeissner/32/58009_2.png) [@fredmeissner](https://www.epiusers.help/u/fredmeissner)\
**Post date:** [October 10, 2025, 6:40pm UTC](https://www.epiusers.help/t/union-2-baq-sub-queries-and-join-the-result-of-that-union-to-another-table/46934/10 "2025-10-10T18:40:04Z")

</div>

I just spent so much time trying to get an AI tool to help me write a union in the designer that does this, I finally gave up and found this thread. You’re the man.
