M
Matt
Hello to anyone who can help,
I have a table of property sales which can contain duplicate
properties and sales (a particular sale (PropID, SalePrice, SaleDate)
can be duplicated, and multiple sales of different prices can appear
on one date for the same property)
I need to select for each property that has a sale, the most recent
sale where the SalePrice > 0. If the property has multiple sales on
the max(Date) then the one with the highest price should be selected.
At the moment I'm using 5 queries to select the required records, but
I think there must be a more efficient way.
Currently using:
qry_MV_Sales_1_Useful: (Removes sales with no price)
SELECT SaleId, PropID, SalePrice, SaleDate FROM tblSales WHERE
SalePrice>0
qry_MV_Sales_2_GroupDate: (Select most recent date of sale for each
prop)
SELECT PropID, Max(SaleDate) AS MaxOfDate
FROM qry_MV_Sales_1_Useful
GROUP BY PropID;
qry_MV_Sales_3_GroupDatePriceSelects highest price)
SELECT S1.PropID, S2.MaxOfDate, Max(S1.SalePrice) AS MaxOfSalePrice
FROM qry_MV_Sales_1_Useful AS S1 INNER JOIN qry_MV_Sales_2_GroupDate
as S2
ON (S1.SaleDate = S2.MaxOfDate) AND (S1.PropID = S2.PropID)
GROUP BY S1.PropID, S2.MaxOfDate;
qry_MV_Sales_4_GroupDatePriceID (Selects last records where sale
duplicated)
SELECT S1.PropID, Max(S1.SaleId) AS MaxOfSaleId1
FROM qry_MV_Sales_1_Useful AS S1 INNER JOIN
qry_MV_Sales_3_GroupDatePrice: as S3
ON (S1.SalePrice = S3.MaxOfSalePrice) AND (S1.PropID = S3.PropID) AND
(S1.SaleDate = S3.MaxOfDate)
GROUP BY S1.PropID;
qry_MV_Sales_5_ToUse
SELECT tblSales.*
FROM qry_MV_Sales_4_GroupDatePriceID AS S4 INNER JOIN tblSales ON
S4.MaxOfSaleId1 = tblSales.SaleId;
If anyone can offer a suggestio to streamline this process, I'd be
very appreciative.
Thanks,
Matt
I have a table of property sales which can contain duplicate
properties and sales (a particular sale (PropID, SalePrice, SaleDate)
can be duplicated, and multiple sales of different prices can appear
on one date for the same property)
I need to select for each property that has a sale, the most recent
sale where the SalePrice > 0. If the property has multiple sales on
the max(Date) then the one with the highest price should be selected.
At the moment I'm using 5 queries to select the required records, but
I think there must be a more efficient way.
Currently using:
qry_MV_Sales_1_Useful: (Removes sales with no price)
SELECT SaleId, PropID, SalePrice, SaleDate FROM tblSales WHERE
SalePrice>0
qry_MV_Sales_2_GroupDate: (Select most recent date of sale for each
prop)
SELECT PropID, Max(SaleDate) AS MaxOfDate
FROM qry_MV_Sales_1_Useful
GROUP BY PropID;
qry_MV_Sales_3_GroupDatePriceSelects highest price)
SELECT S1.PropID, S2.MaxOfDate, Max(S1.SalePrice) AS MaxOfSalePrice
FROM qry_MV_Sales_1_Useful AS S1 INNER JOIN qry_MV_Sales_2_GroupDate
as S2
ON (S1.SaleDate = S2.MaxOfDate) AND (S1.PropID = S2.PropID)
GROUP BY S1.PropID, S2.MaxOfDate;
qry_MV_Sales_4_GroupDatePriceID (Selects last records where sale
duplicated)
SELECT S1.PropID, Max(S1.SaleId) AS MaxOfSaleId1
FROM qry_MV_Sales_1_Useful AS S1 INNER JOIN
qry_MV_Sales_3_GroupDatePrice: as S3
ON (S1.SalePrice = S3.MaxOfSalePrice) AND (S1.PropID = S3.PropID) AND
(S1.SaleDate = S3.MaxOfDate)
GROUP BY S1.PropID;
qry_MV_Sales_5_ToUse
SELECT tblSales.*
FROM qry_MV_Sales_4_GroupDatePriceID AS S4 INNER JOIN tblSales ON
S4.MaxOfSaleId1 = tblSales.SaleId;
If anyone can offer a suggestio to streamline this process, I'd be
very appreciative.
Thanks,
Matt