BAQ outer join problem?

So I’m trying to make a simple BAQ. I have a part (a pulley) that always requires a bushing to go with it. There are some incorrect BOM’s where the bushing isn’t called out. I want to see where a parent assembly called for one part, but not the other. I should be able to do a full outer join with with the same table like shown.

However the SQL being generated is doing a where clause on the results, so the outer join isn’t working right. Does anyone know what I am doing wrong or a workaround to make this work?

select 
	[PartMtl].[PartNum] as [PartMtl_PartNum],
	[PartMtl].[MtlPartNum] as [PartMtl_MtlPartNum],
	[PartMtl1].[PartNum] as [PartMtl1_PartNum],
	[PartMtl1].[MtlPartNum] as [PartMtl1_MtlPartNum]
from Erp.PartMtl as PartMtl
full outer join Erp.PartMtl as PartMtl1 on 
	PartMtl.Company = PartMtl1.Company
	and PartMtl.PartNum = PartMtl1.PartNum
	and ( PartMtl1.MtlPartNum = '603-0-0050'  )

where (PartMtl.MtlPartNum = '604-0-0035')

Results of that query

image

What I would expect.

image

wait … that wasn’t right

1 Like

So it looks I can do a left join, (or a right join if I change the order of the tables) but a full join will not get results both tables unless the rows match. The first table in the list will limit all of the results to existing in that table, and it will show when the second table doesn’t exist, but I can’t get it to go both ways with the full join.

Is this a known limitation? With SQL or the BAQ? or is this something that is a bug that I should report.

If you’re doing this as a one-off, you could include a filed from the 2nd table in the displayed columns. Then look for records with that field being blank

Here’s the output of BOMs that contain PN1. BOM’s w/o PN2 show as the ones with blanks ParentPart

image

Edit: if you make the above a Sub-Query, it could then be filtered on records where ParentPart ISNULL

See if this works

ck-test-outer.baq (32.6 KB)

It’s 3 subs
Sub2 (inner) - finds BOM’s with both Parts
Sub1 (inner) - finds BOMs With Part A, if BOM doesn’t contain Part B, that field will be null
Sub0 (top) - filters Sub1 to show only when Part B is null

What about when B exists and not A?

What you have would basically be a left join, which I can do with one level. (from your description, I haven’t opened up the BAQ yet)

I got the information I needed for this specific problem (hence the excel screen shot), But I’m worried about the full join not doing what a fill join should be doing for future BAQ’s.

I would not be surprised If I added unnecessary layers while trying to get to end game, and then forgot what the end game was :slight_smile:

I can get this with on level. (I jumped to the job table, because I fixed the master.

image

Then if I switch the order of the tables, then I get this.

image

If I’m not mistaken, the full join should work the same no matter the table order right?

I’m no SQL expert. So I don’t know the ins and outs of a FULL join.

Are trying to get BOMs where (has A and Not B) OR (has B and Not A)?

yeah. They are dependent parts. You can’t have one without the other. So just want to see where there is one without the other.

A full join, with criteria should do that, as far as I know. But hopefully someone with more SQL experience can tell me what I should do instead.

There are other areas where I want to check for matching things and show me where they don’t match (both ways) and I’m worried that it won’t work the way I expect.

@knash Any ideas?

Why not this?
create a sq to show job and part A
create a sq to show job and part B
Then a top query to do the outer join

So in English…

BOM’s with (Part A OR Part B) - BOMs with (Part A AND Part B)

That first set of (…) finds any BOM that uses either (which includes both)

The second set of (…) is just the BOMs that contain both

Yeah, so basically, you have to avoid putting table criteria on anything with an outer join, or it will give you problems. If you put the table into a subquery and filter it there first, it will work.

So that’s the workaround.

Does anyone agree that this isn’t how it should work? Or am I just being overly critical?

May I suggest another method that includes no joins?

Create two calculated fields: NumPartA and NumPartB. These fields are numeric and are equal to 1 if the PartMtl is equal to the selected part (A or B parameters) and 0 (zero) if not. Now, subtotal these by Part and Rev.

In the subtotals, if both totals are zero, then you don’t care. If the totals don’t match then you have one part but not another. No links. One run. No errors.

Mark W.

But requires post processing :wink: - to filter out the zeros, no?

What if part A is used twice, and part B none? That gives you a count of 2, no?

No post-processing. Use SubQuery Criteria to check if both calculated fields are non-zero. Remember, there are TWO totals: PartA and PartB, so we’re interested whenever there’s a difference. 2 and 0 is just as wrong as 1 and 0, or 2 and 1.

Mark W.

1 Like

I’m confused with

“Create two calculated fields: NumPartA and NumPartB. These fields are numeric and are equal to 1 if the PartMtl is equal to the selected part (A or B parameters) and 0 (zero) if not. Now, subtotal these by Part and Rev.

How am I subtotaling these two values? like:

SUM(case when PartMtl.MtlPartNum = @PartA then 1 else 0 end)

And then grouping by PartNum and Rev like:

image

Or do I need two more calc fields to sum up NumPartA and NumPartB?

Edit: Adding subquery criteria based on NumPartA and NumPartB generate error:
“An Aggregate may not appear in the WHERE clause …”

Here’s how I would do it. (I did use a subquery, so I’ll have to eat the no-link statement) :wink:

Diff-Test.baq (22.7 KB)

Mark W.

“Can’t import query definition from version latter than current: 3.2.200.0”
:frowning:

But I get the gist of it now.

Whoops. Sorry about that. I can send screen shots if that helps! Just let me know.

Mark W.