Thanks Michel, however, I'm at a bit of a loss following your logic. Below
is
a copy of the (query) SQL statement containing the data for September 26
and
27th. See if you can follow it and build it as you mentioned. That, would
be
a great help to me. If you have any questions, please post back. Here is
the
SQL as it is:
=============
SELECT
tbl_Product_BrandyInfoData_Collection_Temp.ctr_Product_BrandyInfoData_Key,
tbl_Product_BrandyInfoData_Collection_Temp.lng_Customer_Track_fkey,
tbl_Product_BrandyInfoData_Collection_Temp.lng_Liquor_Type_fkey,
tbl_Product_BrandyInfoData_Collection_Temp.lng_Shift_Rotation_fKey,
tbl_Product_BrandyInfoData_Collection_Temp.lng_Customer_Station_fkey,
tbl_Product_BrandyInfoData_Collection_Temp.dte_AuditDte,
tbl_Product_BrandyInfoData_Collection_Temp.tme_AuditTme,
tbl_Product_BrandyInfoData_Collection_Temp.chkBx_StockAdd,
tbl_Product_BrandyInfoData_Collection_Temp.num_Btles_Added,
tbl_Product_BrandyInfoData_Collection_Temp.dte_Stk_AddDte,
tbl_Product_BrandyInfoData_Collection_Temp.num_BtlsOnHand,
tbl_Product_BrandyInfoData_Collection_Temp.num_Liter_Qty,
[num_Liter_Qty]*33.81402 AS NetLiquidOunces,
tbl_Product_BrandyInfoData_Collection_Temp.curr_PlusAdds,
tbl_Product_BrandyInfoData_Collection_Temp.curr_MinusAdds,
tbl_Product_BrandyInfoData_Collection_Temp.str_Par_Level,
tbl_Product_BrandyInfoData_Collection_Temp.str_Btl_Price,
IIf(IsNull([str_Btl_Price]),0,CCur([str_Btl_Price])) AS BottlePrice,
tbl_Product_BrandyInfoData_Collection_Temp.str_Btl_Size,
Format(IIf(IsNull([str_Btl_Size]),Null,CDbl([str_Btl_Size])),"0.00") AS
BottleSize, tbl_Product_BrandyInfoData_Collection_Temp.str_Tare_Wt,
tbl_Product_BrandyInfoData_Collection_Temp.str_Shot_Size,
tbl_Product_BrandyInfoData_Collection_Temp.str_Sell_Price,
IIf(IsNull([str_Shot_Size]),0,CDbl([str_Shot_Size])) AS ShotSize,
[BottleSize]/[ShotSize] AS NumOfShots, [BottlePrice]/[NumOfShots] AS
CostPerShot, [BottlePrice]/[BottleSize] AS CostPerOz,
([NumOfShots]*[str_Sell_Price])*[num_BtlsOnHand] AS ActualOnHndSales,
([NumOfShots]*[str_Sell_Price])*[str_Par_Level] AS PotentialSales,
[str_Par_Level]-[num_BtlsOnHand] AS [Re-OrderQty],
[Re-OrderQty]*[str_Btl_Price] AS OrderValue
FROM tbl_Product_BrandyInfoData_Collection_Temp
WHERE
(((tbl_Product_BrandyInfoData_Collection_Temp.lng_Customer_Track_fkey)=RetrieveReportingCustomerKey())
AND ((tbl_Product_BrandyInfoData_Collection_Temp.dte_AuditDte) Between
RetrieveStartDateKey() And RetrieveEndDateKey()))
ORDER BY
tbl_Product_BrandyInfoData_Collection_Temp.ctr_Product_BrandyInfoData_Key;
===============
This is the field name in the table in which dates are set: "dte_AuditDte"
and "ActualOnHndSales" would contain the dollar values for that day or
previous day's value respectively.
I appreciate all the asistance on this. This SQL string may explain a
better
what I'm trying to accomplish.
Thanks, again!
Michel Walsh said:
Hi,
Assume you have three fields, Account, Amount and Sequence (Sequence can
be
day, week, or a plain number, but without duplicated value).
For each record, you want to find the record for the same Account, but
with
the Sequence value that precede the actual one. The "gap" between the two
sequence values can be different than 1.00.
Here is a solution.
SELECT a.account, a.amount, a.sequence, b.amount, b.sequence
would be what we want get, "a" being the actual record, "b" being the
previous record. Sure, both records come from the same table:
FROM myTable As a LEFT JOIN myTable As b ON a.account=b.account AND
a.sequence > b.sequence
is expected. We use an outer join since the "first" record, in a
sequence,
won't have any "previous" record. An inner join would have remove that
record.
Now, there is a problem, since we pump ALL the previous record (
b.sequence
< a.sequence occurs multiple time). We want b.sequence equal to the
maximum
possible value of sequence, for this group:
HAVING b.sequence = MAX(c.sequence)
but the NULL value creates a problem, that we solve with
HAVING COUNT(*)=1 OR b.sequence = MAX(c.sequence)
"c" is not define, but, as we have explained, it is "as" "b":
FROM (myTable As a LEFT JOIN myTable As b ON a.account=b.account AND
a.sequence > b.sequence)
LEFT JOIN myTable AS c ON a.account=c.account AND a.sequence >
c.sequence
where we can see that the FROM clause does not really differentiate "b"
from
"c", so both "references" reach the same records. While we will keep "b"
to
pump sequence and amount, c is only use to get the MAX value for
sequence.
Since we invoke MAX, we need to define our groups:
GROUP BY a.account, a.sequence. b.sequence
and since the amount fields do not really make part of the groups, we
have
to aggregate them. I personally use LAST in such cases, but MS SQL Server
user could use MIN or MAX for the same purpose, here, in this particular
case. So the overall query is:
=======================
SELECT a.account, LAST(a.amount), a.sequence, LAST(b.amount), b.sequence
FROM (myTable As a LEFT JOIN myTable As b ON a.account=b.account AND
a.sequence > b.sequence)
LEFT JOIN myTable AS c ON a.account=c.account AND a.sequence >
c.sequence
GROUP BY a.account, a.sequence. b.sequence
HAVING COUNT(*)=1 OR b.sequence = MAX(c.sequence)
=======================
Hoping it may help,
Vanderghast, Access MVP