[CareerCup] 15.2 Renting Apartment II 租房之二
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 | 11 |
| 102 | A2 | 12 |
| 103 | A3 | 13 |
| 201 | B1 | 14 |
| 202 | B2 | 15 |
+-------+------------+------------+
-- TABLE Buildings
+------------+-----------+---------------+---------------+
| BuildingID | ComplexID | BuildingName | Address |
+------------+-----------+---------------+---------------+
| 11 | 1 | Eastern Hills | San Diego, CA |
| 12 | 2 | East End | Seattle, WA |
| 13 | 3 | North Park | New York |
| 14 | 4 | South Lake | Orlando, FL |
| 15 | 5 | West Forest | Atlanta, GA |
+------------+-----------+---------------+---------------+
-- TABLE Tenants
+----------+------------+
| TenantID | TenantName |
+----------+------------+
| 1000 | Zhang San |
| 1001 | Li Si |
| 1002 | Wang Wu |
| 1003 | Yang Liu |
+----------+------------+
-- TABLE Complexes
+-----------+---------------+
| ComplexID | ComplexName |
+-----------+---------------+
| 1 | Luxuary World |
| 2 | Paradise |
| 3 | Woderland |
| 4 | Dreamland |
| 5 | 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 | Closed | 103 | |
| 70 | Closed | 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 租房之二的更多相关文章
- [CareerCup] 15.3 Renting Apartment III 租房之三
Building #11 is undergoing a major renovation. Implement a query to close all requests from apartmen ...
- [CareerCup] 15.1 Renting Apartment 租房
Write a SQL query to get a list of tenants who are renting more than one apartment. -- TABLE Apartme ...
- 复旦大学2015--2016学年第二学期(15级)高等代数II期末考试第六大题解答
六.(本题10分) 设 $n$ 阶复方阵 $A$ 的特征多项式为 $f(\lambda)$, 复系数多项式 $g(\lambda)$ 满足 $(f(g(\lambda)),g'(\lambda))= ...
- [CareerCup] 15.7 Student Grade 学生成绩
15.7 Imagine a simple database storing information for students' grades. Design what this database m ...
- [CareerCup] 15.6 Entity Relationship Diagram 实体关系图
15.6 Draw an entity-relationship diagram for a database with companies, people, and professionals (p ...
- [CareerCup] 15.5 Denormalization 逆规范化
15.5 What is denormalization? Explain the pros and cons. 逆规范化Denormalization是一种通过添加冗余数据的数据库优化技术,可以帮助 ...
- [CareerCup] 15.4 Types of Join 各种交
15.4 What are the different types of joins? Please explain how they differ and why certain types are ...
- (使用STL中的数据结构进行编程7.3.15)UVA 630 Anagrams (II)(求一个单词在字典中出现的次数)
/* * UVA_630.cpp * * Created on: 2013年11月4日 * Author: Administrator */ #include <iostream> #in ...
- [LeetCode] Meeting Rooms II 会议室之二
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
随机推荐
- 国内大学毕业论文 LaTeX 模板集合
西北工业大学学位论文LaTeX模板 http://code.google.com/p/nwputhesis/ 西北工业大学硕博士论文LaTeX模版 http://code.google.com/p/n ...
- WebView中实现文件下载功能
WebView控制调用相应的WEB页面进行展示.当碰到页面有下载链接的时候,点击上去是一点反应都没有的.原来是因为WebView默认没有开启文件下载的功能,如果要实现文件下载的功能,需要设置Web ...
- 转载 linux内核 asmlinkage宏
转载http://blog.chinaunix.net/uid-7390305-id-2057287.html 看一下/usr/include/asm/linkage.h里面的定义:#define a ...
- LoadRunner Pacing设置(转)
转载的,备读 在 LoadRunner 的运行场景中,有一个不大起眼的设置,可能经常会被很多人忽略,它就是Pacing .具体设置方式为: Run-Time settings à General à ...
- SpringJMS解析3-监听器
消息监听器容器是一个用于查看JMS目标等待消息到达的特殊bean,一旦消息到达它就可以获取到消息,并通过调用onMessage()方法将消息传递给一个MessageListener实现.Spring中 ...
- 基于netty的微服务架构
基于netty的微服务架构 微服务一篇好文章 http://san-yun.iteye.com/blog/1693759 教程 http://udn.yyuap.com/doc/essential-n ...
- TestNg线程池配置、执行次数配置、超时配置
使用注解的方式对TestNg线程池配置.执行次数配置.超时配置 注:使用注解来控制测试方法运行的次数和超时时间,timeOut在单线程或者多线程模式下都可用,threadPoolSize设置了线程池的 ...
- AngularJS html+DOM+ng-click事件
ng-disabled 指令直接绑定应用程序数据到 HTML 的 disabled 属性. ng-show 指令用于设置应用部分是否可见. ng-show="true" 设置 HT ...
- AngularJs基础(一)
使用 angularjs首先在页面的<html>里添加一个模块写法: <html lang="en"ng-app="myApp"> my ...
- Android HttpClient post MultipartEntity - Android 上传文件
转自[http://blog.csdn.net/hellohaifei/article/details/9707089] 在Android 中使用HttpClient,MultipartEntity ...