Adding a few tables to a BAQ / Bad SQL Statement

Hello!

I have a BAQ that someone made for us and I would like to add three tables to do it.

When I do, I get the “Severity: Error, Text: Bad SQL statement.” response.

The circled are the three I added. Otherwise, when they are not there, the data successfully populates.

Specifically, I want:

PartOpr.CommentText
Part.PartDescription
PartMtl.MtlPartNum
PartMtl.RelatedOperation

I figure this is an issue with joins but I struggle with knowing what to do to fix or how to experiment to fix this type of problem. Any advice is appreciated.

Thank you.

LaborDetailParts.baq (50.6 KB)

The first calculated field has a divide by 0 issue.

and so does the second.

And so does the 3rd.
image

Bringing in those extra tables is probably bringing in extra rows that end up having 0’s so you get a bad calculation.

5 Likes

Yeah, like @Banderson said… I just pulled it in an ran it (after changing it to not be updateable and cross-company) and it ran fine for me. So, probably your data conflicting with your calc fields.

1 Like

What do you mean by “pulled it in”? I unchecked updateable and cross-company and am unfortunately still experiencing the error.

(I also tried to see if the error would clear if I removed those calculated fields, which I may not even need, but alas same result)

I just mean I imported into my system so I could look at it.

Let me ask you a question.

Sometimes when it seems to take a long time to load, I hit cancel/“x” and it generates that SQL error I mentioned.

However, I gave it time this time to find all the records. It populated with information and did not produce the SQL error.

What is happening here? Is my SQL still wonky? Do I cause an error in the coding when I prematurely cancel it?

Ah I see there is still a problem.

There are duplicate lines.

How do I reduce instances of duplicates?

Set your top level to Distinct (or any relevant subqueries).

I did, however it returns “Severity: Error, Text: Bad SQL statement.” and nothing populates.

while this isn’t wrong… it’s not right either… Setting this to distinct is generally a band aid when you don’t understand the data. Sure it has it’s purpose, but if you don’t know exactly why you are getting duplicates, isn’t the best idea.

You have duplicates because the part mtl and partOpr tables have multiple rows of data related to the job assembly.

Also, what data are you trying to get? Because it’s probably in the job tables and you’ll run into potential problems looking at the master instead.

3 Likes

Completely agree. It works. It’s not the right answer most of the time. Best practice is to try to eliminate (or understand) the duplicates based on the tables and fields you added.

1 Like

I have a user that requested a few columns be added to this Labor Detail we have. I think they all come from the Method Tracker screen:

PartOpr.CommentText
Part.PartDescription
PartMtl.MtlPartNum
PartMtl.RelatedOperation

All of that is in the job tables. Use JobOp and JobMtl and JobAsm.

3 Likes

I’ve found the most useful reasons for using distinct are

A) When I need a quick band-aid to fix a BAQ that is pulling duplicate rows, and someone needs it fixed quickly so their dashboard is correct, or something along those lines. I’ve run into this a lot when I have to make a fix on someone else’s BAQ, and I want to get the user accurate data quickly. Then I have the time to go back and correct the actual Query structure.

B) In a subquery when I need to tie two tables together but, but a direct relationship doesn’t exist. For example, if I wanted to get the Shipment date(s) for an order, I’d join OrderHed > ShipDtl > ShipHead to get ShipHead.ShipDate, but that would introduce a new row for every shipment line. I can use distinct to only return the ShipDate(s) tied to that order.

2 Likes

Just finding those fields in the preexisting tables helped. I have what I need. Thank you!

1 Like

Glad we could get you straightened out. Knowing the schema structure is something that just takes time working with the system.