标签:
Write a SQL query to get a list of all buildings and the number of open requests (Requests in which status equals ‘Open‘).
-- TABLE Apartments
+-------+------------+------------+
| AptID | UnitNumber | BuildingID |
+-------+------------+------------+
| 101 | A1 | 1 |
| 102 | A2 | 2 |
| 103 | A3 | 3 |
| 201 | B1 | 4 |
| 202 | B2 | 5 |
+-------+------------+------------+
-- TABLE Buildings
+------------+-----------+---------------+---------------+
| BuildingID | ComplexID | BuildingName | Address |
+------------+-----------+---------------+---------------+
| 1 | 11 | Eastern Hills | San Diego, CA |
| 2 | 12 | East End | Seattle, WA |
| 3 | 13 | North Park | New York |
| 4 | 14 | South Lake | Orlando, FL |
| 5 | 15 | West Forest | Atlanta, GA |
+------------+-----------+---------------+---------------+
-- TABLE Tenants
+----------+------------+
| TenantID | TenantName |
+----------+------------+
| 1000 | Zhang San |
| 1001 | Li Si |
| 1002 | Wang Wu |
| 1003 | Yang Liu |
+----------+------------+
-- TABLE Complexes
+-----------+---------------+
| ComplexID | ComplexName |
+-----------+---------------+
| 11 | Luxuary World |
| 12 | Paradise |
| 13 | Woderland |
| 14 | Dreamland |
| 15 | LostParis |
+-----------+---------------+
-- TABLE AptTenants
+----------+-------+
| TenantID | AptID |
+----------+-------+
| 1000 | 102 |
| 1001 | 102 |
| 1002 | 101 |
| 1002 | 103 |
| 1002 | 201 |
| 1003 | 202 |
+----------+-------+
-- TABLE Requests
+-----------+--------+-------+-------------+
| RequestID | Status | AptID | Description |
+-----------+--------+-------+-------------+
| 50 | Open | 101 | |
| 60 | Close | 103 | |
| 70 | Close | 102 | |
| 80 | Open | 201 | |
| 90 | Open | 202 | |
+-----------+--------+-------+-------------+
这道题让我们返回所有的building,并标记出来每个building有多少个Open的requests,那么我们首先要计算每个building的Open的request的个数,然后再和Buildings表联合返回对应的BuildingName,因为Requests表里对应的是Apartment和request,而一个Building里可能有很多个Apartment,所以我们先要联合Apartments表和Requests表来计算每个building的Open请求的个数,我们用内交Inner Join来做,通过AptID列来内交Apartments表和Requests表,然后通过BuildingID来群组,并生成一个名为Count的列,然后再用Buildings表和Count列左交,这里需要注意下,如果某个building没有Open请求,那么我们需要返回0,即需要把NULL变为0,在MySQL里面我们用IFNULL函数来做,而SQL Server则用ISNULL,Oracle则用NVL,详细对比可参见这里。参见代码如下:
SELECT BuildingName, IFNULL(Count, 0) AS ‘Count‘ FROM Buildings LEFT JOIN (SELECT Apartments.BuildingID, COUNT(*) AS ‘Count‘ FROM Requests INNER JOIN Apartments ON Requests.AptID = Apartments.AptID WHERE Requests.Status = ‘Open‘ GROUP BY Apartments.BuildingID) ReqCounts ON ReqCounts.BuildingID = Buildings.BuildingID;
运行结果:
+---------------+-------+ | BuildingName | Count | +---------------+-------+ | Eastern Hills | 1 | | East End | 0 | | North Park | 0 | | South Lake | 1 | | West Forest | 1 | +---------------+-------+
[CareerCup] 15.2 Renting Apartment II 租房之二
标签:
原文地址:http://www.cnblogs.com/grandyang/p/5351362.html