Full outer join very slow - sql

I have two tables. One is todays prices and one is yesterdays prices. There is a job which updates each table over night. Yesterdays prices is a copy of todays before it gets updated.
I am trying to create a table which shows the differences between the tables.
To do this I am using a full outer join. I am filtering down my criteria to make it faster as both tables are over 48 million rows.
My current code is like this.
WITH differ AS
(
SELECT
tAP.CustomerID, tAP.ProductID, yAP.CustomerID AS 'Yest_CustomerID', yAP.ProductID AS 'Yest_ProductID',
tAP.PDG, tAP.DiscPct1, tAP.DiscPct2,
yAP.DiscPct1 AS 'Yest_DiscPct1',
yAP.DiscPct2 AS 'Yest_DiscPct2',
CONVERT(DECIMAL(18,2),tAP.DiscPct1 - yAP.DiscPct1) AS 'DiscPct1_Difference',
CONVERT(DECIMAL(18,2),tAP.DiscPct2 - yAP.DiscPct2) AS 'DiscPct2_Difference',
tAP.internalSPANumber, yAP.internalSPANumber AS 'Yest_InternalSPANumber', tAP.SPAUniqueReference,
tAP.Project,
tAP.ExpiryDate,tAP.Subaddress, tAP.PriceType, yAP.PriceType AS 'Yest_PriceType', tAP.ListPrice,
tAP.NettPrice, yAP.NettPrice AS 'Yest_NettPrice',
CONVERT(DECIMAL(18,2),tAP.NettPrice- yAP.NettPrice) AS 'NettPrice_Difference'
FROM tbl_Prices tAP FULL OUTER JOIN tbl_Prices_Yesterday yAP ON tAP.CustomerId = yAP.CustomerID AND tAP.ProductID = yAP.ProductID
AND tAP.PDG = yAP.PDG AND tAP.Project = yAP.Project AND tAP.PriceType = yAP.PriceType
WHERE (((tAP.DiscPct1 <> yAP.DiscPct1)
OR (tAP.DiscPct2 <> yAP.DiscPct2)
OR (yAP.CustomerID IS NULL)
OR (tAP.CustomerID IS NULL)
OR (tAP.NettPrice <> yAP.NettPrice)
OR (tAP.InternalSPANumber <> yAP.InternalSPANumber))
AND
(
tAP.ProductID = '10238610' OR tAP.ProductID = '10238620'
OR tAP.ProductID = '10238621' OR tAP.ProductID = '10238687'
OR tAP.ProductID = '10238688' OR yAP.ProductID = '10238610'
OR yAP.ProductID = '10238620' OR yAP.ProductID = '10238621'
OR yAP.ProductID = '10238687' OR yAP.ProductID = '10238688')
)
)
SELECT * INTO tbl_Difference FROM differ
Anyway to make this faster?

Can you filter the 2 large tables before you try to join them using AND filter in your where?
WITH tAPcte AS
(
SELECT * -- fields you need
FROM tbl_Prices
WHERE ProductID IN ('10238610',
'10238620',
'10238621',
'10238687',
'10238688')
),
yAPcte AS
(
SELECT * -- fields you need
FROM tbl_Prices_Yesterday
WHERE ProductID IN ('10238610',
'10238620',
'10238621',
'10238687',
'10238688')
)
differ AS
(
SELECT
tAP.CustomerID, tAP.ProductID, yAP.CustomerID AS 'Yest_CustomerID', yAP.ProductID AS 'Yest_ProductID',
tAP.PDG, tAP.DiscPct1, tAP.DiscPct2,
yAP.DiscPct1 AS 'Yest_DiscPct1',
yAP.DiscPct2 AS 'Yest_DiscPct2',
CONVERT(DECIMAL(18,2),tAP.DiscPct1 - yAP.DiscPct1) AS 'DiscPct1_Difference',
CONVERT(DECIMAL(18,2),tAP.DiscPct2 - yAP.DiscPct2) AS 'DiscPct2_Difference',
tAP.internalSPANumber, yAP.internalSPANumber AS 'Yest_InternalSPANumber', tAP.SPAUniqueReference,
tAP.Project,
tAP.ExpiryDate,tAP.Subaddress, tAP.PriceType, yAP.PriceType AS 'Yest_PriceType', tAP.ListPrice,
tAP.NettPrice, yAP.NettPrice AS 'Yest_NettPrice',
CONVERT(DECIMAL(18,2),tAP.NettPrice- yAP.NettPrice) AS 'NettPrice_Difference'
FROM tAPcte tAP FULL OUTER JOIN yAPcte yAP ON tAP.CustomerId = yAP.CustomerID AND tAP.ProductID = yAP.ProductID
AND tAP.PDG = yAP.PDG AND tAP.Project = yAP.Project AND tAP.PriceType = yAP.PriceType
WHERE (((tAP.DiscPct1 <> yAP.DiscPct1)
OR (tAP.DiscPct2 <> yAP.DiscPct2)
OR (yAP.CustomerID IS NULL)
OR (tAP.CustomerID IS NULL)
OR (tAP.NettPrice <> yAP.NettPrice)
OR (tAP.InternalSPANumber <> yAP.InternalSPANumber))
)
SELECT * INTO tbl_Difference FROM differ

Related

Too long query time execution

I use this query in order to calculate year to date. But it took too much time to be executed.
How to optimize it?
SELECT T.[Sales_Organization],
T.[CD_DossierMagnitude],
T.[DS_DossierMagnitude],
T.[Sales_Product_Name_N3],
T.[Sales_Product_Name_N2],
T.[Sales_Product_Name_N1],
T.[Market_Segment_Name_N2],
T1.*
FROM stg.Fact_EPV_SEFPRO_DC T
CROSS APPLY (SELECT SUM([QTY]) AS QTY_YTD,
SUM([QTY_UoM]) AS QTY_UoM_YTD,
SUM([PNV_LC]) AS PNV_LC_YTD,
SUM([Vmvu]) AS Vmvu_YTD
FROM stg.Fact_DC
WHERE [Delivery_Year] = T.[Delivery_Year]
AND Delivery_month <= t.Delivery_month
AND Sales_Organization = T.Sales_Organization
AND Sales_Product_Name_N3 = T.Sales_Product_Name_N3
AND Sales_Product_Name_N2 = T.Sales_Product_Name_N2
AND Sales_Product_Name_N1 = T.Sales_Product_Name_N1
AND Market_Segment_Name_N2 = T.Market_Segment_Name_N2
AND Market_Grp = T.Market_Grp
AND Product_Name_SOA = T.Product_Name_SOA
AND Doc_Currency = T.Doc_Currency) t1

LINQ to Entities Right Join VB.NET

I am trying to figure out how to do a right join in vb.net I have tried several different approaches but neither works.
Dim query = From I In db.scll_label Where I.scll_transactiondate >= fromDate And I.scll_transactiondate <= toDate
Join p In db.pt_mstr.Where(Function(pt) pt.pt_domain = "mueller") On I.scll_part Equals p.pt_part
Join c In db.sclws_cfg.Where(Function(wk) wk.sclws_domain = "mueller") On I.scll_wsid Equals c.sclws_id
Select New ShiftAdjustedModel With
{.Label = I,
.TransactionDate = I.scll_transactiondate,
.PartLength = p.pt_length,
.PartNetWeight = p.pt_net_wt,
.PartDescription = p.pt_desc1,
.PartTolHigh = p.pt_tol_high,
.PartType = p.pt_part_type,
.PartUM = p.pt_um,
.ProjCode = c.sclws_proj_code,
.Site = c.sclws_site}
output sql
SELECT
[Extent1].[scll_ticket] AS [scll_ticket],
[Extent1].[scll_domain] AS [scll_domain],
[Extent1].[scll_site] AS [scll_site],
[Extent1].[scll_part] AS [scll_part],
[Extent1].[scll_qty] AS [scll_qty],
[Extent1].[scll_weight] AS [scll_weight],
[Extent1].[scll_transactiondate] AS [scll_transactiondate],
[Extent1].[scll_transactiontime] AS [scll_transactiontime],
[Extent1].[scll_shift] AS [scll_shift],
[Extent1].[scll_pcs_bundle] AS [scll_pcs_bundle],
[Extent1].[scll_bundle_lift] AS [scll_bundle_lift],
[Extent1].[scll_cust] AS [scll_cust],
[Extent1].[scll_wsid] AS [scll_wsid],
[Extent1].[scll_userid] AS [scll_userid],
[Extent1].[scll_remarks] AS [scll_remarks],
[Extent1].[scll_calc_weight] AS [scll_calc_weight],
[Extent1].[scll_total_feet] AS [scll_total_feet],
[Extent1].[scll_tolerance] AS [scll_tolerance],
[Extent1].[scll_drawlite_factor] AS [scll_drawlite_factor],
[Extent1].[scll_total_tare] AS [scll_total_tare],
[Extent1].[scll_tare_detail] AS [scll_tare_detail],
[Extent1].[scll_out_of_tolerance] AS [scll_out_of_tolerance],
[Extent1].[scll_tolerance_low] AS [scll_tolerance_low],
[Extent1].[scll_tolerance_high] AS [scll_tolerance_high],
[Extent1].[scll_std_weight] AS [scll_std_weight],
[Extent2].[pt_length] AS [pt_length],
[Extent2].[pt_net_wt] AS [pt_net_wt],
[Extent2].[pt_desc1] AS [pt_desc1],
[Extent2].[pt_tol_high] AS [pt_tol_high],
[Extent2].[pt_part_type] AS [pt_part_type],
[Extent2].[pt_um] AS [pt_um],
[Extent3].[sclws_proj_code] AS [sclws_proj_code],
[Extent3].[sclws_site] AS [sclws_site]
FROM [dbo].[scll_label] AS [Extent1]
INNER JOIN [dbo].[pt_mstr] AS [Extent2] ON [Extent1].[scll_part] = [Extent2].[pt_part]
INNER JOIN [dbo].[sclws_cfg] AS [Extent3] ON [Extent1].[scll_wsid] = [Extent3].[sclws_id]
WHERE ([Extent1].[scll_transactiondate] >= #p__linq__0) AND ([Extent1].[scll_transactiondate] <= #p__linq__1) AND ('mueller' = [Extent2].[pt_domain]) AND ('mueller' = [Extent3].[sclws_domain])
if i use this query
Dim query = From I In db.scll_label Where I.scll_transactiondate >= fromDate And I.scll_transactiondate <= toDate
Group Join p In db.pt_mstr.Where(Function(pt) pt.pt_domain = "mueller") On I.scll_part Equals p.pt_part Into parts = Group
Group Join c In db.sclws_cfg.Where(Function(wk) wk.sclws_domain = "mueller") On I.scll_wsid Equals c.sclws_id Into workstations = Group
From p In parts.DefaultIfEmpty From c In workstations.DefaultIfEmpty
Select New ShiftAdjustedModel With
{.Label = I,
.TransactionDate = I.scll_transactiondate,
.PartLength = p.pt_length,
.PartNetWeight = p.pt_net_wt,
.PartDescription = p.pt_desc1,
.PartTolHigh = p.pt_tol_high,
.PartType = p.pt_part_type,
.PartUM = p.pt_um,
.ProjCode = c.sclws_proj_code,
.Site = c.sclws_site}
i get this output
SELECT
[Extent1].[scll_ticket] AS [scll_ticket],
[Extent1].[scll_domain] AS [scll_domain],
[Extent1].[scll_site] AS [scll_site],
[Extent1].[scll_part] AS [scll_part],
[Extent1].[scll_qty] AS [scll_qty],
[Extent1].[scll_weight] AS [scll_weight],
[Extent1].[scll_transactiondate] AS [scll_transactiondate],
[Extent1].[scll_transactiontime] AS [scll_transactiontime],
[Extent1].[scll_shift] AS [scll_shift],
[Extent1].[scll_pcs_bundle] AS [scll_pcs_bundle],
[Extent1].[scll_bundle_lift] AS [scll_bundle_lift],
[Extent1].[scll_cust] AS [scll_cust],
[Extent1].[scll_wsid] AS [scll_wsid],
[Extent1].[scll_userid] AS [scll_userid],
[Extent1].[scll_remarks] AS [scll_remarks],
[Extent1].[scll_calc_weight] AS [scll_calc_weight],
[Extent1].[scll_total_feet] AS [scll_total_feet],
[Extent1].[scll_tolerance] AS [scll_tolerance],
[Extent1].[scll_drawlite_factor] AS [scll_drawlite_factor],
[Extent1].[scll_total_tare] AS [scll_total_tare],
[Extent1].[scll_tare_detail] AS [scll_tare_detail],
[Extent1].[scll_out_of_tolerance] AS [scll_out_of_tolerance],
[Extent1].[scll_tolerance_low] AS [scll_tolerance_low],
[Extent1].[scll_tolerance_high] AS [scll_tolerance_high],
[Extent1].[scll_std_weight] AS [scll_std_weight],
[Extent2].[pt_length] AS [pt_length],
[Extent2].[pt_net_wt] AS [pt_net_wt],
[Extent2].[pt_desc1] AS [pt_desc1],
[Extent2].[pt_tol_high] AS [pt_tol_high],
[Extent2].[pt_part_type] AS [pt_part_type],
[Extent2].[pt_um] AS [pt_um],
[Extent3].[sclws_proj_code] AS [sclws_proj_code],
[Extent3].[sclws_site] AS [sclws_site]
FROM [dbo].[scll_label] AS [Extent1]
LEFT OUTER JOIN [dbo].[pt_mstr] AS [Extent2] ON ('mueller' = [Extent2].[pt_domain]) AND ([Extent1].[scll_part] = [Extent2].[pt_part])
LEFT OUTER JOIN [dbo].[sclws_cfg] AS [Extent3] ON ('mueller' = [Extent3].[sclws_domain]) AND ([Extent1].[scll_wsid] = [Extent3].[sclws_id])
WHERE ([Extent1].[scll_transactiondate] >= #p__linq__0) AND ([Extent1].[scll_transactiondate] <= #p__linq__1)
I simply want a right outer join on the two tables it translated into a left outer join. Even if I start with the tables for the right join as a lot of posts have suggested I get weird sql using cross joins and unions instead of right join.

write SQL join query using Querysets in Django

am trying to write join query on two tables using django Queryset,so need suggestions to write
SQL join query using Querysets in Django..
here is My Query and Models of Tables
SELECT t.time, d.id2
FROM disp_b_time t JOIN disp_dispatch d ON t.id1 = d.id2
WHERE t.status = "completed" AND d.status = 0 AND d.vehicle_id =1
class B_Time(models.Model):
id1 = models.ForeignKey(Book)
dispatcher= models.ForeignKey(D)
status = models.CharField(max_length=128, choices=B_STATUS)
time = models.DateTimeField(auto_now=True, auto_now_add=True)
register = models.DateField()
modified = models.DateTimeField(auto_now=True, auto_now_add=True)
class Dispatch(models.Model):
id2 = models.ForeignKey(Book)
vehicle = models.ForeignKey(Vehicle)
driveId = models.ForeignKey(Drive)
dispId = models.ForeignKey(D)
status = models.CharField(max_length=128, choices=STATUS)
register = models.DateTimeField()
modified = models.DateTimeField(auto_now=True, auto_now_add=True)
Thanks in Advance...
d = Dipatch.objects.filter(vehicle__pk=1)\
.filter(status=0).filter(dispID__b_time__status='completed')
Why
d = Dipatch.objects.filter(vehicle__pk=1)\
.filter(status=0).filter(dispID__b_time__status='completed')
?
We can simply write it as
d = Dipatch.objects.filter(vehicle__pk=1, status=0, dispID__b_time__status='completed')

How do you add an item to a Unionized SQL View?

I am working to make an automated report in Crystal Reports which requires me to add in a table to an existing, pre-written view. The view contains a Union operation which is preventing me from using the view as normal. What I have done is manualy add-in the data (about halfway through).
The item required for the report is tblSOPartsUsed.Memo. which i have added into BOTH select queries. In the join between tblCustomerInventory and tblSOPartsUsed the .Memo table only appears in the PartsUsed table, not in the CustomerInventory.
First Select Query Location
tblCustomerInventory.SerialNumber AS ExchangeSerialNumber, **tblSOPartsUsed.Memo AS SOPartsNotes**, tblInvoiceDetail.Taxable AS DetailIsTaxable,
First From Query Location
tblCustomerInventory FULL OUTER JOIN
tblSOPartsUsed INNER JOIN
I have outlined the second Select and From in exactly the same way.
Can someone please shed some light, i've tried EVERYTHING!
The full code is below...
SELECT InvoiceAssemblyPriceBook.Features AS AssemblyFeatures,
tblInvoiceAssemblyDetail.EachQuantity AS AssemblyEachQuantity,
tblInvoiceAssemblyDetail.FKInvoiceDetail AS AssemblyFKInvoiceDetail,
tblInvoiceAssemblyDetail.InvoiceAssemblyDetailKeyID,
tblInvoiceAssemblyDetail.ItemID AS AssemblyItemID,
tblInvoiceAssemblyDetail.ItemDescription AS AssemblyItemDescription,
tblInvoiceAssemblyDetail.PrintOnInvoice AS AssemblyPrintOnInvoice,
tblInvoiceAssemblyDetail.Quantity AS AssemblyQuantity,
tblInvoiceAssemblyDetail.SellingPrice AS AssemblySellingPrice,
tblInvoiceAssemblyDetail.TotalSellingPrice AS AssemblyTotalSellingPrice,
tblInvoiceAssemblyDetail.Type AS AssemblyType,
tblInvoiceAssemblyDetail.UnitOfMeasure AS AssemblyUnitOfMeasure,
tblInvoiceDetail.AssemblyType AS DetailAssemblyType,
tblInvoiceDetail.InvoiceDetailKeyID,
tblInvoiceDetail.ItemDescription AS DetailItemDescription,
tblInvoiceDetail.ItemID AS DetailItemID,
tblInvoiceDetail.PrintOnInvoice AS DetailPrintOnInvoice,
tblInvoiceDetail.Quantity AS DetailQuantity,
tblInvoiceDetail.SellingPrice AS DetailSellingPrice,
tblInvoiceDetail.TotalSellingPrice AS DetailTotalSellingPrice,
tblInvoiceDetail.Type AS DetailType,
tblInvoices.AccountNumber,
tblInvoices.Comments,
tblInvoices.ContractNumber,
tblInvoices.Deposit,
tblInvoices.Freight,
tblInvoices.GSTax,
tblInvoices.InvoiceDate,
tblInvoices.InvoiceNumber,
tblInvoices.PaidDate,
tblInvoices.QuoteNumber,
tblInvoices.SalesTaxPercent,
CASE
WHEN tblInvoiceDetail.SoNumber IS NULL
THEN tblInvoices.SONumber
ELSE tblInvoiceDetail.SoNumber
END AS SONumber,
tblInvoices.STATUS,
tblInvoices.StatusDate,
tblInvoices.Tax,
tblInvoices.TotalAmountDue,
tblInvoices.TotalComment,
tblInvoices.TotalDollarsDiscounted,
tblInvoices.TotalGrossSell,
tblInvoices.TotalNetSell,
tblInvoices.TradeIn,
tblInvoices.WorkOrderNumber,
tblPriceLevels.IsRepairLevel,
tblServiceOrders.ContractNumber AS ServiceOrderContractNumber,
tblSysCompanySettings.HideGSTaxRelatedInformation,
tblSysCompanySettings.ItemsServicedPrintOnInvoice,
tblSysDisclaimerSettings.SalesInvoiceDisclaimer,
tblSysDisclaimerSettings.ServiceInvoiceDisclaimer,
tblSysPBSettings.PrintItemorPartNum,
VoidedByReps.RepName AS VoidedByRepName,
tblInvoices.TotalNetInvoice,
InvoiceDetailPriceBook.PartNumber AS DetailPartNumber,
InvoiceDetailPriceBook.UnitOfMeasure AS DetailUnitOfMeasure,
InvoiceDetailPriceBook.Features AS DetailFeatures,
tblSysDisclaimerSettings.ContractInvoiceDisclaimer,
tblInvoices.ProviderTax,
tblServiceOrders.BriefDescription AS SOBriefDescription,
tblInvoices.GSTaxComputedBeforeTradeIn,
tblInvoices.TaxComputedBeforeTradeIn,
tblInvoices.ProviderTaxRate,
tblInvoices.FreightTaxable,
tblInvoices.GSTIsTaxable,
tblInvoices.ProjectKeyID,
InvoicesReps.RepName,
tblAccounts.AccountNumber AS Expr1,
tblAccounts.AccountID,
tblInvoices.Terms,
tblInvoices.Reference,
tblInvoices.ARCustomerNumber,
tblInvoices.PONumber,
tblInvoices.ShipVia,
tblServiceOrders.DateRequested,
tblServiceOrders.DateOpened,
tblServiceOrders.SONumber AS ServiceOrderSONumber,
tblSysReportSettings.InvoiceCommentsAtEnd,
tblInvoices.SourceDocument,
tblTaxCodes.HasTieredDistrict,
tblExchange.ExchangeKeyID,
tblCustomerInventory.ItemID AS ExchangeItemID,
tblCustomerInventory.ItemDescription AS ExchangeItemDescription,
tblCustomerInventory.SerialNumber AS ExchangeSerialNumber,
tblSOPartsUsed.Memo AS SOPartsNotes,
tblInvoiceDetail.Taxable AS DetailIsTaxable,
tblInvoiceAssemblyDetail.Taxable AS AssemblyIsTaxable,
tblInvoices.IsFinalProgressiveInvoice,
tblInvoiceDetail.IsProgressiveInvoiceItem,
tblInvoices.IsProgressiveInvoice,
tblInvoices.TotalPriceCredited,
tblInvoices.TotalTaxCredited,
tblInvoices.TotalGSTaxCredited,
tblInvoices.TotalProviderTaxCredited,
tblInvoices.TotalFreightCredited,
tblInvoices.DiscountAllowed,
tblInvoices.AmountPaid,
(
SELECT MAX(SOItemsServicedKeyID) AS Expr1
FROM tblSOItemsServiced
WHERE (SONumber = tblServiceOrders.SONumber)
) AS SOItemsServicedKeyID,
tblInvoiceDetail.CommentOnly,
ServiceOrderContacts.ContactName AS SOContactName,
tblServiceOrders.ContactPhone AS SOContactPhone,
tblServiceOrders.ContactPhoneLocation AS SOContactPhoneLocation,
(
SELECT TOP (1) FormattedPhoneNumber
FROM tblPhoneNumbers
WHERE (ContactNumber = 0)
AND (PrimaryIndicator = 1)
AND (COALESCE(PhoneLocation, '') <> 'Fax')
AND (AccountNumber = tblAccounts.AccountNumber)
) AS AccountPhone,
(
SELECT TOP (1) PhoneLocation
FROM tblPhoneNumbers AS tblPhoneNumbers_1
WHERE (ContactNumber = 0)
AND (PrimaryIndicator = 1)
AND (COALESCE(PhoneLocation, '') <> 'Fax')
AND (AccountNumber = tblAccounts.AccountNumber)
) AS AccountPhoneLocation,
COALESCE(tvwr_TotalAmountDuePerAccount.AmountDue, 0.00) AS AmountDue,
COALESCE(tvwr_TotalAmountDuePerAccount.Unappliedpayments, 0.00) AS Unappliedpayments,
tblTaxCodes.IsHarmonizedTaxCode,
tblInvoices.GSTax + tblInvoices.Tax AS HSTax
FROM tblPriceBook AS InvoiceAssemblyPriceBook
RIGHT JOIN tblPriceBook AS InvoiceDetailPriceBook
RIGHT JOIN tblInvoiceAssemblyDetail
RIGHT JOIN tblCustomerInventory
FULL JOIN tblSOPartsUsed
INNER JOIN tblExchange
ON tblSOPartsUsed.SOPartsUsedKeyID = tblExchange.FKSOPartsUsed
ON tblCustomerInventory.CustomerInventoryKeyID = tblExchange.FKCustomerInventory RIGHT JOIN tblInvoiceDetail
ON tblSOPartsUsed.FKInvoiceDetail = tblInvoiceDetail.InvoiceDetailKeyID
ON tblInvoiceAssemblyDetail.FKInvoiceDetail = tblInvoiceDetail.InvoiceDetailKeyID
ON InvoiceDetailPriceBook.ItemID = tblInvoiceDetail.ItemID
ON InvoiceAssemblyPriceBook.ItemID = tblInvoiceAssemblyDetail.ItemID LEFT JOIN tblPriceLevels
ON tblInvoiceDetail.PriceLevel = tblPriceLevels.PriceLevelsKeyID RIGHT JOIN tvwr_TotalAmountDuePerAccount INNER JOIN tblAccounts
ON tvwr_TotalAmountDuePerAccount.AccountNumber = tblAccounts.AccountNumber RIGHT JOIN tblReps AS VoidedByReps RIGHT JOIN tblInvoices AS tblInvoices LEFT JOIN tblTaxCodes
ON tblInvoices.SalesTaxCode = tblTaxCodes.SalesTaxCode LEFT JOIN tblReps AS InvoicesReps
ON tblInvoices.SalesRep = InvoicesReps.RepNumber
ON VoidedByReps.RepNumber = tblInvoices.StatusBy LEFT JOIN tblServiceOrders LEFT JOIN tblContacts AS ServiceOrderContacts
ON tblServiceOrders.ContactNumber = ServiceOrderContacts.ContactNumber
ON tblInvoices.SONumber = tblServiceOrders.SONumber
ON tblAccounts.AccountNumber = tblInvoices.AccountNumber
ON tblInvoiceDetail.InvoiceNumber = tblInvoices.InvoiceNumber LEFT JOIN tblContacts AS tblContacts
ON tblAccounts.PrimaryContactNumber = tblContacts.ContactNumber CROSS JOIN tblSysPBSettings CROSS JOIN tblSysCompanySettings CROSS JOIN tblSysReportSettings CROSS JOIN tblSysDisclaimerSettings
WHERE (tblInvoices.MSPAgreementNumber = 0)
UNION ALL
SELECT InvoiceAssemblyPriceBook.Features AS AssemblyFeatures,
tblInvoiceAssemblyDetail.EachQuantity AS AssemblyEachQuantity,
tblInvoiceAssemblyDetail.FKInvoiceDetail AS AssemblyFKInvoiceDetail,
tblInvoiceAssemblyDetail.InvoiceAssemblyDetailKeyID,
tblInvoiceAssemblyDetail.ItemID AS AssemblyItemID,
tblInvoiceAssemblyDetail.ItemDescription AS AssemblyItemDescription,
tblInvoiceAssemblyDetail.PrintOnInvoice AS AssemblyPrintOnInvoice,
tblInvoiceAssemblyDetail.Quantity AS AssemblyQuantity,
tblInvoiceAssemblyDetail.SellingPrice AS AssemblySellingPrice,
tblInvoiceAssemblyDetail.TotalSellingPrice AS AssemblyTotalSellingPrice,
tblInvoiceAssemblyDetail.Type AS AssemblyType,
tblInvoiceAssemblyDetail.UnitOfMeasure AS AssemblyUnitOfMeasure,
tblInvoiceDetail.AssemblyType AS DetailAssemblyType,
tblInvoiceDetail.InvoiceDetailKeyID,
tblInvoiceDetail.ItemDescription AS DetailItemDescription,
tblInvoiceDetail.ItemID AS DetailItemID,
tblInvoiceDetail.PrintOnInvoice AS DetailPrintOnInvoice,
tblInvoiceDetail.Quantity AS DetailQuantity,
tblInvoiceDetail.SellingPrice AS DetailSellingPrice,
tblInvoiceDetail.TotalSellingPrice AS DetailTotalSellingPrice,
tblInvoiceDetail.Type AS DetailType,
tblInvoices.AccountNumber,
tblInvoices.Comments,
tblInvoices.ContractNumber,
tblInvoices.Deposit,
tblInvoices.Freight,
tblInvoices.GSTax,
tblInvoices.InvoiceDate,
tblInvoices.InvoiceNumber,
tblInvoices.PaidDate,
tblInvoices.QuoteNumber,
tblInvoices.SalesTaxPercent,
CASE
WHEN tblInvoiceDetail.SoNumber IS NULL
THEN tblInvoices.SONumber
ELSE tblInvoiceDetail.SoNumber
END AS SONumber,
tblInvoices.STATUS,
tblInvoices.StatusDate,
tblInvoices.Tax,
tblInvoices.TotalAmountDue,
tblInvoices.TotalComment,
tblInvoices.TotalDollarsDiscounted,
tblInvoices.TotalGrossSell,
tblInvoices.TotalNetSell,
tblInvoices.TradeIn,
tblInvoices.WorkOrderNumber,
tblPriceLevels.IsRepairLevel,
tblServiceOrders.ContractNumber AS ServiceOrderContractNumber,
tblSysCompanySettings.HideGSTaxRelatedInformation,
tblSysCompanySettings.ItemsServicedPrintOnInvoice,
tblSysDisclaimerSettings.SalesInvoiceDisclaimer,
tblSysDisclaimerSettings.MSPAgreementInvoiceDisclaimer AS ServiceInvoiceDisclaimer,
tblSysPBSettings.PrintItemorPartNum,
VoidedByReps.RepName AS VoidedByRepName,
tblInvoices.TotalNetInvoice,
InvoiceDetailPriceBook.PartNumber AS DetailPartNumber,
InvoiceDetailPriceBook.UnitOfMeasure AS DetailUnitOfMeasure,
InvoiceDetailPriceBook.Features AS DetailFeatures,
tblSysDisclaimerSettings.ContractInvoiceDisclaimer,
tblInvoices.ProviderTax,
tblServiceOrders.BriefDescription AS SOBriefDescription,
tblInvoices.GSTaxComputedBeforeTradeIn,
tblInvoices.TaxComputedBeforeTradeIn,
tblInvoices.ProviderTaxRate,
tblInvoices.FreightTaxable,
tblInvoices.GSTIsTaxable,
tblInvoices.ProjectKeyID,
InvoicesReps.RepName,
tblAccounts.AccountNumber AS Expr1,
tblAccounts.AccountID,
tblInvoices.Terms,
tblInvoices.Reference,
tblInvoices.ARCustomerNumber,
tblInvoices.PONumber,
tblInvoices.ShipVia,
tblServiceOrders.DateRequested,
tblServiceOrders.DateOpened,
tblServiceOrders.SONumber AS ServiceOrderSONumber,
tblSysReportSettings.InvoiceCommentsAtEnd,
tblInvoices.SourceDocument,
tblTaxCodes.HasTieredDistrict,
tblExchange.ExchangeKeyID,
tblCustomerInventory.ItemID AS ExchangeItemID,
tblCustomerInventory.ItemDescription AS ExchangeItemDescription,
tblCustomerInventory.SerialNumber AS ExchangeSerialNumber,
tblSOPartsUsed.Memo AS SOPartsNotes,
tblInvoiceDetail.Taxable AS DetailIsTaxable,
tblInvoiceAssemblyDetail.Taxable AS AssemblyIsTaxable,
tblInvoices.IsFinalProgressiveInvoice,
tblInvoiceDetail.IsProgressiveInvoiceItem,
tblInvoices.IsProgressiveInvoice,
tblInvoices.TotalPriceCredited,
tblInvoices.TotalTaxCredited,
tblInvoices.TotalGSTaxCredited,
tblInvoices.TotalProviderTaxCredited,
tblInvoices.TotalFreightCredited,
tblInvoices.DiscountAllowed,
tblInvoices.AmountPaid,
(
SELECT MAX(SOItemsServicedKeyID) AS Expr1
FROM tblSOItemsServiced
WHERE (SONumber = tblServiceOrders.SONumber)
) AS SOItemsServicedKeyID,
tblInvoiceDetail.CommentOnly,
ServiceOrderContacts.ContactName AS SOContactName,
tblServiceOrders.ContactPhone AS SOContactPhone,
tblServiceOrders.ContactPhoneLocation AS SOContactPhoneLocation,
(
SELECT TOP (1) FormattedPhoneNumber
FROM tblPhoneNumbers
WHERE (ContactNumber = 0)
AND (PrimaryIndicator = 1)
AND (COALESCE(PhoneLocation, '') <> 'Fax')
AND (AccountNumber = tblAccounts.AccountNumber)
) AS AccountPhone,
(
SELECT TOP (1) PhoneLocation
FROM tblPhoneNumbers AS tblPhoneNumbers_1
WHERE (ContactNumber = 0)
AND (PrimaryIndicator = 1)
AND (COALESCE(PhoneLocation, '') <> 'Fax')
AND (AccountNumber = tblAccounts.AccountNumber)
) AS AccountPhoneLocation,
COALESCE(tvwr_TotalAmountDuePerAccount.AmountDue, 0.00) AS AmountDue,
COALESCE(tvwr_TotalAmountDuePerAccount.Unappliedpayments, 0.00) AS Unappliedpayments,
COALESCE(tblTaxCodes.IsHarmonizedTaxCode, 0) AS IsHarmonizedTaxCode,
tblInvoices.GSTax + tblInvoices.Tax AS HSTax
FROM tblPriceBook AS InvoiceAssemblyPriceBook
RIGHT JOIN tblPriceBook AS InvoiceDetailPriceBook
RIGHT JOIN tblInvoiceAssemblyDetail
RIGHT JOIN tblCustomerInventory
FULL JOIN tblSOPartsUsed
INNER JOIN tblExchange
ON tblSOPartsUsed.SOPartsUsedKeyID = tblExchange.FKSOPartsUsed
ON tblCustomerInventory.CustomerInventoryKeyID = tblExchange.FKCustomerInventory RIGHT JOIN tblInvoiceDetail
ON tblSOPartsUsed.FKInvoiceDetail = tblInvoiceDetail.InvoiceDetailKeyID
ON tblInvoiceAssemblyDetail.FKInvoiceDetail = tblInvoiceDetail.InvoiceDetailKeyID
ON InvoiceDetailPriceBook.ItemID = tblInvoiceDetail.ItemID
ON InvoiceAssemblyPriceBook.ItemID = tblInvoiceAssemblyDetail.ItemID LEFT JOIN tblPriceLevels
ON tblInvoiceDetail.PriceLevel = tblPriceLevels.PriceLevelsKeyID RIGHT JOIN tvwr_TotalAmountDuePerAccount INNER JOIN tblAccounts
ON tvwr_TotalAmountDuePerAccount.AccountNumber = tblAccounts.AccountNumber RIGHT JOIN tblReps AS VoidedByReps RIGHT JOIN tblInvoices AS tblInvoices LEFT JOIN tblTaxCodes
ON tblInvoices.SalesTaxCode = tblTaxCodes.SalesTaxCode LEFT JOIN tblReps AS InvoicesReps
ON tblInvoices.SalesRep = InvoicesReps.RepNumber
ON VoidedByReps.RepNumber = tblInvoices.StatusBy LEFT JOIN tblServiceOrders LEFT JOIN tblContacts AS ServiceOrderContacts
ON tblServiceOrders.ContactNumber = ServiceOrderContacts.ContactNumber
ON tblInvoices.SONumber = tblServiceOrders.SONumber
ON tblAccounts.AccountNumber = tblInvoices.AccountNumber
ON tblInvoiceDetail.InvoiceNumber = tblInvoices.InvoiceNumber LEFT JOIN tblContacts AS tblContacts
ON tblAccounts.PrimaryContactNumber = tblContacts.ContactNumber CROSS JOIN tblSysPBSettings CROSS JOIN tblSysCompanySettings CROSS JOIN tblSysReportSettings CROSS JOIN tblSysDisclaimerSettings
WHERE (tblInvoices.MSPAgreementNumber <> 0)
I managed to copy each half of the view into a new view, manually tick off the tblSOPartsUsed.memo from the GUI and copied the two together which made the view work great

How to join three tables?

SELECT
PC_SL_ACNO, -- DB ITEM
SLNAME, -- ACCOUNT NAME:
SL_TOTAL_AMOUNT -- TOTAL AMOUNT:
FROM GLAS_PDC_CHEQUES
WHERE PC_COMP_CODE=:parameter.COMP_CODE
AND pc_bank_from = :block02.pb_bank_code
AND pc_due_date between :block01.date_from
AND :block01.date_to
AND nvl(pc_discd,'X') IN(‘X’, 'R')
GROUP BY
pc_comp_code, pc_sl_ldgr_code, pc_sl_acno
ORDER BY pc_sl_acno
ACCOUNT NAME:
BEGIN
SELECT COAD_PTY_FULL_NAME INTO :BLOCK03.SLNAME
FROM GLAS_PTY_ADDRESS,GLAS_SBLGR_MASTERS
WHERE SLMA_COMP_CODE = :PARAMETER.COMP_CODE
AND SLMA_ADDR_ID = COAD_ADDR_ID
AND SLMA_ADDR_TYPE = COAD_ADDR_TYPE
AND SLMA_ACNO = :BLOCK03.PC_SL_ACNO
AND SLMA_COMP_CODE = COAD_COMP_CODE;
EXCEPTION WHEN OTHERS THEN NULL;
END;
TOTAL AMOUNT:
BEGIN
SELECT SUM(PC_AMOUNT) INTO :SL_TOTAL_AMOUNT
FROM GLAS_PDC_CHEQUES
WHERE PC_DUE_DATE BETWEEN :BLOCK01.DATE_FROM AND :BLOCK01.DATE_TO
AND PC_BANK_FROM = :block02.PB_BANK_CODE
AND PC_SL_ACNO = :BLOCK03.PC_SL_ACNO
AND NVL(PC_DISCD,'X') = 'R'
AND PC_COMP_CODE = :PARAMETER.COMP_CODE;
EXCEPTION WHEN OTHERS THEN :block03.SL_TOTAL_AMOUNT := 0;
END;
How can I join the three tables?
You'll have to adjust depending on precisely what criteria and required fields you have for your query or queries.
SELECT
c.PC_SL_ACNO,
a.COAD_PTY_FULL_NAME,
SUM(c.PC_AMOUNT)
FROM
GLAS_PDC_CHEQUES c
LEFT JOIN
GLAS_SBLGR_MASTERS m
ON ( c.PC_SL_ACNO = m.SLMA_ACNO
AND c.PC_COMP_CODE = m.SLMA_COMP_CODE
)
LEFT JOIN
GLAS_PTY_ADDRESS a
ON ( m.SLMA_ADDR_ID = a.COAD_ADDR_ID
AND m.SLMA_COMP_CODE = a.COAD_COMP_CODE
AND m.SLMA_ADDR_TYPE = a.COAD_ADDR_TYPE
)
WHERE
c.PC_COMP_CODE = :PARAMETER.COMP_CODE
AND c.PC_SL_ACNO = :BLOCK03.PC_SL_ACNO
AND c.PC_BANK_FROM = :BLOCK02.PB_BANK_CODE
AND NVL(c.PC_DISCD,'X') IN (‘X’, 'R')
AND c.PC_DUE_DATE BETWEEN :BLOCK01.DATE_FROM AND :BLOCK01.DATE_TO
GROUP BY
c.PC_SL_ACNO, -- not sure which grouping exactly you need.
a.COAD_PTY_FULL_NAME
ORDER BY
c.PC_SL_ACNO
I notice that in the first query you have pc_comp_code as a search criterion, and on the leading edge of your grouping - is that what you need?
This is a bit of an 'estimate' due to the enigmatic nature of your question!