我有一个用于房地产系统的数据库,我正在尝试生成一个结果集,其中包含系统中每个办公室的所有列表中最高的列表价格。它会返回与正确的办公室相关的正确的最高上市价格,但地址/城市/州与最高上市价格的结果不相关。
这是我的SQL:
SELECT o.office_name, l.address, l.city, l.state, (SELECT MAX(l.listing_price)) AS highest_listing_price
FROM offices o
JOIN agent_assignments aa
ON o.office_key = aa.office_key
JOIN client_listings cl
ON aa.assignment_key = cl.assignment_key
JOIN listings l
ON cl.listing_key = l.listing_key
GROUP BY o.office_name;
这是它给出的结果:
+---------------------+------------------+--------------+-------+-----------------------+
| office_name | address | city | state | highest_listing_price |
+---------------------+------------------+--------------+-------+-----------------------+
| Seqouia Real Estate | 838 Plum Rd | Jasper | IL | 679000.00 |
| Mirage Property | NULL | Orchardville | IN | 739000.00 |
| Excelsior Estates | NULL | Sandlake | IL | 605300.00 |
| Silverwood Realty | 399 Apricot Ave | Roseville | IL | 475000.00 |
| Hearthstone Nooks | 290 Highridge Dr | Roseville | IL | 925000.00 |
+---------------------+------------------+--------------+-------+-----------------------+
这是我用于此查询的表中的信息:
DROP TABLE IF EXISTS offices;
CREATE TABLE offices
(
office_key INT PRIMARY KEY,
office_name VARCHAR(50),
address VARCHAR(50),
city VARCHAR(30),
state CHAR(2),
zip_code VARCHAR(10),
phone_number VARCHAR(10),
fax_number VARCHAR(10),
email_address VARCHAR(50),
remarks VARCHAR(1000)
);
DROP TABLE IF EXISTS agent_assignments;
CREATE TABLE agent_assignments
(
assignment_key INT PRIMARY KEY,
agent_key INT REFERENCES agents (agent_key),
office_key INT REFERENCES offices (office_key),
office_manager_flag CHAR(1),
date_assigned DATE,
end_date DATE
);
DROP TABLE IF EXISTS listings;
CREATE TABLE listings
(
listing_key INT PRIMARY KEY,
listing_status INT REFERENCES listing_status (listing_status_key),
listing_type INT REFERENCES listing_types (listing_type_key),
date_listed DATE,
date_expires DATE,
date_unlisted DATE,
reason_unlisted INT REFERENCES unlisted_reasons (unlisted_reason_key),
address VARCHAR(50),
city VARCHAR(30),
state CHAR(2),
zip_code VARCHAR(10),
lot_number VARCHAR(50),
residential_area INT REFERENCES residential_areas (residential_area_key),
listing_price DECIMAL(13, 2),
listing_agreement_signed_date CHAR(1),
remarks VARCHAR(1000)
);
DROP TABLE IF EXISTS client_listings;
CREATE TABLE client_listings
(
client_listing_key INT PRIMARY KEY,
listing_key INT REFERENCES listings (listing_key),
assignment_key INT REFERENCES agent_assignments (assignment_key),
client_key INT REFERENCES clients (client_key)
);
我尝试过的一件事是使用它来关联地址,但是却导致错误“子查询返回多行”:
WHERE l.address = (SELECT l.address FROM listings l WHERE l.listing_price = (SELECT MAX(l.listing_price)))
答案 0 :(得分:1)
您需要首先标识具有最高上市价格的记录,然后获取所有必需的列。您的查询仅识别出标价的最大值,而不识别与其相关的其他列。
尝试一下:
set @rownum := 0;
select * from (SELECT o.office_name, l.address, l.city, l.state, l.listing_price, (@rownum := @rownum + 1) AS rn
FROM offices o
JOIN agent_assignments aa
ON o.office_key = aa.office_key
JOIN client_listings cl
ON aa.assignment_key = cl.assignment_key
JOIN listings l
ON cl.listing_key = l.listing_key
order by l.listing_price desc)
where rn=1;
答案 1 :(得分:0)
使用窗口功能:
SELECT o.*
FROM (SELECT o.office_name, l.address, l.city, l.state,
l.listing_price AS highest_listing_price
ROW_NUMBER() OVER (PARTITION BY o.office_name ORDER BY l.listing_price DESC) as seqnum
FROM offices o JOIN
agent_assignments aa
ON o.office_key = aa.office_key JOIN
client_listings cl
ON aa.assignment_key = cl.assignment_key JOIN
listings l
ON cl.listing_key = l.listing_key
) o
WHERE seqnum = 1;