21agdgcf
asked on
Select a distinct and max vale from a table
Hi Experts
I am trying select rows form a table that are distinct and have a max date value ie
Account Property Value Valuation_Date
Freehold Property - Cost B/F 12 Tysoe Street 370000.00 2008-09-30 00:00:00
Freehold Property - Cost B/F 12 Tysoe Street 370000.00 2008-12-31 00:00:00
Freehold Property - Cost B/F 12 Tysoe Street 370000.00 2009-03-31 00:00:00 *
Freehold Property - Cost B/F 21 Hamilton House 951000.00 2008-12-31 00:00:00
Freehold Property - Cost B/F 21 Hamilton House 951000.00 2008-09-30 00:00:00 *
I need to be able to select the stared rows based on the max date value and for each property
ie I only get one row for each property based on the max date
I have tried this
SELECT vw_Accounts.name AS Account, properties.address AS Property, property_valuations.curren t_value,
property_valuations.valuat ion_date AS [Valuation Date]
FROM properties INNER JOIN
property_valuations ON properties.property_id = property_valuations.proepr ty_id INNER JOIN
vw_Accounts ON property_valuations.Accoun t = vw_Accounts.account_id
WHERE (properties.IsSold = 0) AND (property_valuations.valua tion_date =
(SELECT MAX(valuation_date) AS Expr1
FROM property_valuations AS property_valuations_1))
however this returns the wrong records ie the max date only
Any ideas?
Thanks
I am trying select rows form a table that are distinct and have a max date value ie
Account Property Value Valuation_Date
Freehold Property - Cost B/F 12 Tysoe Street 370000.00 2008-09-30 00:00:00
Freehold Property - Cost B/F 12 Tysoe Street 370000.00 2008-12-31 00:00:00
Freehold Property - Cost B/F 12 Tysoe Street 370000.00 2009-03-31 00:00:00 *
Freehold Property - Cost B/F 21 Hamilton House 951000.00 2008-12-31 00:00:00
Freehold Property - Cost B/F 21 Hamilton House 951000.00 2008-09-30 00:00:00 *
I need to be able to select the stared rows based on the max date value and for each property
ie I only get one row for each property based on the max date
I have tried this
SELECT vw_Accounts.name AS Account, properties.address AS Property, property_valuations.curren
property_valuations.valuat
FROM properties INNER JOIN
property_valuations ON properties.property_id = property_valuations.proepr
vw_Accounts ON property_valuations.Accoun
WHERE (properties.IsSold = 0) AND (property_valuations.valua
(SELECT MAX(valuation_date) AS Expr1
FROM property_valuations AS property_valuations_1))
however this returns the wrong records ie the max date only
Any ideas?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can achieve your expected results either by the way acperkins posted above. Or like below
In your query, the sub-query always returns the same value ie., the max of property_valuations.valuat ion_date. This cause wrong results in your query.
You have modify your query, the sub-query including a WHERE condition that will select only the max date of the particular property_id of each row only.
I have no actual tables to test with. Here I tried to modify your query including the WHERE condition in your sub-query. Check it.
If there is any addition condition to match, add it. Now add property_id as you want to get max date based on this field.
Raj
In your query, the sub-query always returns the same value ie., the max of property_valuations.valuat
You have modify your query, the sub-query including a WHERE condition that will select only the max date of the particular property_id of each row only.
I have no actual tables to test with. Here I tried to modify your query including the WHERE condition in your sub-query. Check it.
If there is any addition condition to match, add it. Now add property_id as you want to get max date based on this field.
Raj
SELECT vw_Accounts.name AS Account,
properties.address AS Property,
pv.current_value,
pv.valuation_date AS [Valuation Date]
FROM properties
INNER JOIN property_valuations pv ON properties.property_id = pv.property_id
INNER JOIN vw_Accounts ON pv.Account = vw_Accounts.account_id
WHERE (properties.IsSold = 0) AND
(pv.valuation_date =
(SELECT MAX(valuation_date) AS Expr1
FROM property_valuations AS property_valuations_1
WHERE property_id = pv.property_id))
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi acperkins
Many thanks for that
I think I undderstadn the logic to apply elsewhere now
Many thanks for that
I think I undderstadn the logic to apply elsewhere now
ASKER
Again many thanks
ASKER
You are right I have misspelt it however i get the error
No column was specified for column 2 of 'd'.No column was specified for column 2 of 'd'.