Question

new to SQL (Searched my problem but couldn't find anything :() I'm hoping this is something simple... Basically I'm trying to query a simple table of real estate properties which have Price, Address etc as well as Property Type (Detached, Semi, Apartment). I'm asked to return the Highest and Lowest priced property for every property type. Easy right? I thought so, and still managed to mess up!

So my current query is this:

  SELECT Area, 
         Address, 
         Property_Code, 
         Property_Type, 
         Price, 
         Market_Date, 
         Sold
    FROM tbl_Sale_Property
   WHERE (
           Price IN (
                      SELECT MIN(Price) 
                        FROM tbl_Sale_Property 
                    GROUP BY Property_Type
                    ) 
           OR Price IN (
                      SELECT MAX(Price) 
                        FROM tbl_Sale_Property 
                    GROUP BY Property_Type
                    )
          )
ORDER BY Property_Type;

Which from my (very limited) experience should return every min/max priced record for each property type. My thinking was that if it just checked whether the price was a MIN or MAX and sorted it after, this would work. Unfortunately, for some reason, it returns more than two records for some of the property types- Semi-Detached returns:

  • £120,000.00
  • £210,000.00
  • £210,000.00
  • £210,000.00
  • £380,000.00

When it should return only the highest and lowest numbers.Any help would be greatly appreciated! I apologise again if this has been answered previously or is super mind numbingly simple! I'm using Access 2007-2010.

Was it helpful?

Solution

The problem that you have is that you are comparing to the lowest/largest prices for all property types. So, property type 1 could match the lowest price of property type 2, and it will be in your output.

You want correlated subqueries:

SELECT Area, Address, Property_Code, Property_Type, Price, Market_Date, Sold
FROM tbl_Sale_Property sp
WHERE sp.Price = (SELECT MIN(Price) FROM tbl_Sale_Property sp2 where sp.Property_Type = sp2.Property_Type) or
      sp.Price = (SELECT MAX(Price) FROM tbl_Sale_Property sp2 where sp.Property_Type = sp2.Property_Type)
ORDER BY Property_Type;

You also have the simple issue that more than one record might have the minimum value. Is your problem that you need to limit the results to one out of several minimum/maximum prices?

OTHER TIPS

If you just need a list of property types and then the lowest sale price and highest sales price for each then try the following:

SELECT Property_Type, MIN(Price) AS LowestPrice, MAX(Price) AS HighestPrice
FROM tbl_Sale_Property AS sp
GROUP BY Property_Type
SELECT MIN(Price) FROM tbl_Sale_Property GROUP BY Property_Type

1) You need an order by in your where clause as the order isn't gaurenteed
2) This will select the min price for each type of property record you have. ie; 5 property records 5 min prices.

So your where clause has the potential to look like this

where price in(1,2,3,4,5)

when what you really want is where price in (1,5)

to fix that

SELECT MIN(Price) FROM tbl_Sale_Property 

That will get the absolute minimum price in the table

Same logic applies to the SELECT MAX

FIXED

SELECT Area, Address, Property_Code, Property_Type, Price, Market_Date, Sold
FROM tbl_Sale_Property
WHERE (Price = (SELECT MIN(Price) FROM tbl_Sale_Property ) 
OR Price = (SELECT MAX(Price) FROM tbl_Sale_Property ))

If you want to just find the lowest and highest priced properties

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top