Query /Subquery Help Needed

P

Phonon

The story so far: Parent Table SalesOrders having primary Key SFSOID,
and a couple of child tables (One to many) Receivables which stores the
SFSOID in a column for each invoice cut against it.

Problem: when I wrote a query with a
SubQuery selecting only receivables within a given date range (say the month
of June). The query returns an "extra row" that doesn't even match the date
range criteria!.
I was thinking I could start with the QuerybyDateinReceivables then
grab the related Sales orders from that.. Kinda Bottom up rather than top
down. Does this approach make sense?

Query:
SELECT DISTINCT SalesOrders.IsComplete, SalesOrders.SFSOID,
SalesOrders.OrderDate, Customers.CompanyName, Employees.LastName,
Employees.FirstName, Employees.CommissionRate, (SELECT Sum(AmountPaid) AS
TotalReceivables FROM Receivables WHERE
[Receivables].[SFSOID]=[SalesOrders].[SFSOID] AND
(((Receivables.DatePaid)>=[forms]![Report Date Range]![Beginning Order Date]
And (Receivables.DatePaid)<=[forms]![Report Date Range]![Ending Order
Date]))) AS TotalReceivables, (SELECT Sum(OriginalAmount) AS TotalPaid FROM
Payables WHERE Payables.SFSOID = SalesOrders.SFSOID) AS TotalPaid, (SELECT
Sum(FreightCoInvoiceAmt) AS TotalPaidFreight FROM Payables WHERE
Payables.SFSOID = SalesOrders.SFSOID) AS TotalPaidFreight,
Format(Nz([CCCharge],0),"Currency") AS CC,
Format(Nz([Adjustments],0),"Currency") AS AJ,
Format(Nz([BestWayNJ],0),"Currency") AS BWNJ,
Format(Nz([WestCoastCA],0),"Currency") AS WCCA,
Format(Nz([BarFreightIL],0),"Currency") AS BRIL,
Format(Nz([ConsolidatedTX],0),"Currency") AS CSTX,
Format(Nz([EvansMI],0),"Currency") AS EVMI,
Format(Nz([Surcharge],0),"Currency") AS SC,
Format(nZ([BestWayNJ],0)+nZ([WestCoastCA],0)+nZ([BarFreightIL],0)+nZ([ConsolidatedTX],0)+nZ([EvansMI],0)+nZ([Surcharge],0),"Currency") AS TotalWarehouse
FROM (Employees INNER JOIN (Customers INNER JOIN (SalesOrders LEFT JOIN
Payables ON SalesOrders.SFSOID = Payables.SFSOID) ON Customers.CustomerID =
SalesOrders.CustomerID) ON Employees.EmployeeID = SalesOrders.EmployeeID)
LEFT JOIN Receivables ON SalesOrders.SFSOID = Receivables.SFSOID
WHERE (((SalesOrders.IsComplete)=True))
ORDER BY Employees.LastName;


Sorry it is a beast.

Help?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Query/Subquery Help 0

Top