Hi there,
I have been working for a while now on a dashboard to analyze our quoted, planned, and actual costs for orders/jobs that are in our database. The idea behind this is to calculate costs and get revenue information from the Quote, Order, and Job in the same line. The basic structure that I think I need, before adding in the QuoteMtl, JobMtl, QuoteOpr, etc. tables and group is as follows:
At the moment, I’m just selected the key fields from each of these tables to test the validity of the structure. You can find the query text at the end of this post.
The issue that I’m having is due to the fact that the QuoteDtl table can join to JobHead if details are gotten from the linked quote and/or OrderDtl if Get Opportunity / Quote is used to create the sales order line. I need both of these joins to be present in the query in order to maintain the link between QuoteDtl-OrderDtl when a quote has been converted into a Sales Order but no Job has been created yet or the Job’s details were not pulled in from the linked quote.
I have tried using only one of the joins but that causes the QuoteDtl records to pull in separately from the JobHead or OrderDtl record, depending on which join I keep and which one I remove.
The order of the tables also clearly matters here. When I have the order as shown in the screenshot above, the QuoteDtl-OrderDtl record does not pull in properly in that the QuoteDtl record comes in alone and the OrderDtl fields are null. When I reverse the order (QuoteDtl → Order Dtl → Order Rel → …) that QuoteDtl-OrderDtl record comes in fine but the JobProd record for standalone Jobs (make to stock) do not pull in.
I appreciate any help you can provide on this, I consider myself decent at writing SQL queries but this one really has me stumped.
Query Text:
select
[OrderRel].[OrderNum] as [OrderRel_OrderNum],
[OrderRel].[OrderLine] as [OrderRel_OrderLine],
[OrderRel].[OrderRelNum] as [OrderRel_OrderRelNum],
[OrderDtl].[OrderNum] as [OrderDtl_OrderNum],
[OrderDtl].[OrderLine] as [OrderDtl_OrderLine],
[JobProd].[JobNum] as [JobProd_JobNum],
[JobProd].[OrderNum] as [JobProd_OrderNum],
[JobProd].[OrderLine] as [JobProd_OrderLine],
[JobProd].[OrderRelNum] as [JobProd_OrderRelNum],
[JobHead].[JobNum] as [JobHead_JobNum],
[JobHead].[QuoteNum] as [JobHead_QuoteNum],
[JobHead].[QuoteLine] as [JobHead_QuoteLine],
[QuoteDtl].[QuoteNum] as [QuoteDtl_QuoteNum],
[QuoteDtl].[QuoteLine] as [QuoteDtl_QuoteLine]
from Erp.QuoteDtl as QuoteDtl
full outer join Erp.OrderDtl as OrderDtl on
OrderDtl.Company = QuoteDtl.Company
and OrderDtl.QuoteNum = QuoteDtl.QuoteNum
and OrderDtl.QuoteLine = QuoteDtl.QuoteLine
full outer join Erp.OrderRel as OrderRel on
OrderDtl.Company = OrderRel.Company
and OrderDtl.OrderNum = OrderRel.OrderNum
and OrderDtl.OrderLine = OrderRel.OrderLine
full outer join Erp.JobProd as JobProd on
OrderRel.Company = JobProd.Company
and OrderRel.OrderNum = JobProd.OrderNum
and OrderRel.OrderLine = JobProd.OrderLine
and OrderRel.OrderRelNum = JobProd.OrderRelNum
full outer join Erp.JobHead as JobHead on
QuoteDtl.Company = JobHead.Company
and QuoteDtl.QuoteNum = JobHead.QuoteNum
and QuoteDtl.QuoteLine = JobHead.QuoteLine
full outer join Erp.JobHead as JobHead
and
JobProd.Company = JobHead.Company
and JobProd.JobNum = JobHead.JobNum