If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > [help] query!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-11-11, 02:42
michellemabelle michellemabelle is offline
Registered User
 
Join Date: Apr 2011
Posts: 5
[help] query!

Hi to everyone!
I'd need some help with this query:

I have to find the city with highest average price/mq of buildings with private use among cities of the same region.

This is what I've written till now:

Code:
SELECT address.region, address.city, AVG(announce.price/building.mq) 
FROM announce JOIN building ON (announce.id_i = building.id_i)
JOIN address ON (building.id_i = address.id_i)
WHERE bulding.use = 'private' AND price IS NOT NULL
GROUP BY address.region, address.city
ORDER BY address.region, address.city
With this code I find the average price/mq for every city of every region.
Since I can't use something like MAX(AVG(announce.price/building.mq)) in Postgres,
how can I find a city for every region?!
Reply With Quote
  #2 (permalink)  
Old 04-11-11, 03:02
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Code:
SELECT region, 
       city, 
       max(avg_price) 
FROM (
    SELECT address.region, address.city, AVG(announce.price/building.mq) as avg_price
    FROM announce JOIN building ON (announce.id_i = building.id_i)
    JOIN address ON (building.id_i = address.id_i)
    WHERE bulding.use = 'private' AND price IS NOT NULL
    GROUP BY address.region, address.city
) t
GROUP BY region, city
ORDER BY region, city
Reply With Quote
  #3 (permalink)  
Old 04-11-11, 03:09
michellemabelle michellemabelle is offline
Registered User
 
Join Date: Apr 2011
Posts: 5
I tried but the result is the same of my previous query!
I need the city with the highest cost among his region, but I got all the cities of all the regions!
Reply With Quote
  #4 (permalink)  
Old 04-11-11, 03:37
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Code:
SELECT adr1.region, 
       adr1.city, 
       AVG(ann1.price/b1.mq) as avg_price
FROM annnonce ann1 
   JOIN building b1 ON (ann1.id_i = b1.id_i)
   JOIN address adr1 ON (b1.id_i = adr1.id_i)
WHERE b1.use = 'private' 
  AND ann1.price IS NOT NULL
GROUP BY address.region, address.city
HAVING AVG(ann1.price/b1.mq) = (SELECT max(ann2.price/b2.mq)
                                FROM announce ann2 
                                   JOIN building b2 ON (ann2.id_i = b2.id_i)
                                   JOIN address adr2 ON (b2.id_i = adr2.id_i)
                                WHERE adr2.region = adr1.region
                                  AND ann2.price IS NOT NULL)
GROUP BY adr1.region, adr1.city
Reply With Quote
  #5 (permalink)  
Old 04-11-11, 03:59
michellemabelle michellemabelle is offline
Registered User
 
Join Date: Apr 2011
Posts: 5
Tried.
But I got only a result (and neither the max result!).

Theorically my result should be 2, 'cos I have 4 cities in 2 different regions:

Lombardia - Milano
Lombardia - Busto
Toscana - Bolgheri
Toscana - Livorno

And my results should be:

Lombardia - Milano
Toscana - Livorno
Reply With Quote
  #6 (permalink)  
Old 04-11-11, 04:10
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Post the table definition as CREATE TABLE statements
Post sample data as INSERT INTO statements
Reply With Quote
  #7 (permalink)  
Old 04-11-11, 04:13
michellemabelle michellemabelle is offline
Registered User
 
Join Date: Apr 2011
Posts: 5
The problem is that my tables are in Italian (I've translated everything here)
and i don't have the insert into 'cos I've inserted the data via website
Reply With Quote
  #8 (permalink)  
Old 04-11-11, 05:22
michellemabelle michellemabelle is offline
Registered User
 
Join Date: Apr 2011
Posts: 5
SOLVED!

TRANSLATION:
regione = region
comune = city
annuncio = announce
prezzo = price
immobile = building
recapito = address
uso = use
privato = private

SELECT r1.regione, r1.comune, AVG(annuncio.prezzo/immobile.mq) AS costo_medio
FROM annuncio JOIN immobile ON (annuncio.id_i = immobile.id_i)
JOIN recapito r1 ON (immobile.id_i = r1.id_i)
WHERE immobile.uso = 'privato' AND prezzo IS NOT NULL
GROUP BY r1.regione, r1.comune
HAVING AVG(annuncio.prezzo/immobile.mq) > ALL
(SELECT AVG(annuncio.prezzo/immobile.mq)
FROM annuncio JOIN immobile ON (annuncio.id_i = immobile.id_i)
JOIN recapito r2 ON (immobile.id_i = r2.id_i)
WHERE r1.regione = r2.regione
)
ORDER BY r1.regione, r1.comune
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On