[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 ...
随机推荐
- 函数fseek() 用法(转)
在阅读代码时,遇到了很早之前用过的fseek(),很久没有用了,有点陌生,写出来以便下次查阅. 函数功能是把文件指针指向文件的开头,需要包含头文件stdio.h fseek 函数名: fseek ...
- CentOS安装中文支持
部分文档突然成乱码了. 解决方法: 1.安装中文支持包 # yum groupinstall "Chinese Support" 2 修改# /etc/sysconfig/i18n ...
- Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
C. Inna and Candy Boxes Inna loves sweets very much. She has n closed present boxes lines up in a ...
- 虚拟机下玩DXF
DXF检测虚拟机好象已经很长时间了,记得当时也是在网上找的教程,今天无聊又检测了一下,发现目前依然有效.用记事本打开 虚拟机启动文件 xxxx.vmx 在最后添加如下两行代码monitor_contr ...
- loadrunner实现字符串的替换
char *replace_str(char *str, char *orig, char *rep) { static char buffer[9096]; char *p; i ...
- Adobe Flash Media Server安装
Flash Media Server(FMS)是一个流媒体服务器 使用 实时消息传送协议(RTMP),RTMP是一种未加密的TCP/IP协议,专门设计用来高速传送音频.视频和数据信息. 3.5版32位 ...
- HTTP协议开发应用-文件服务器
HTTP(超文本传输协议)协议是建立在TCP传输协议之上的应用层协议.HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统. 本文将重点介绍如何基于Netty的 ...
- localhost和127.0.0.1 的区别
- Graphics 导出图片使用【这个主要是画图类图的使用,记录一下】
/// <summary> /// 导出信令流程矢量图 /// </summary> /// <param name="signalFlowInfos" ...
- sring mvc 返回值至jsp界面的几种方式
Spring 通过Controller 向 View 传值的方法有以下四种 HttpServletRequest ModelAndView Map<String, Object> map ...