[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         |          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 All in One 题目汇总

时间: 2024-10-09 22:04:12

[CareerCup] 15.2 Renting Apartment II 租房之二的相关文章

[CareerCup] 15.1 Renting Apartment 租房

Write a SQL query to get a list of tenants who are renting more than one apartment. -- TABLE Apartments +-------+------------+------------+ | AptID | UnitNumber | BuildingID | +-------+------------+------------+ | 101 | A1 | 1 | | 102 | A2 | 2 | | 10

15.文件系统——软RAID的实现(二)(mdadm,watch, RAID1)

前文介绍了使用mdadm命令穿件软RAID0的过程,本章将继续演示RAID1和RAID5的创建过程. 在演示具体的创建过程之前,需要进一步介绍madam命令可用的选项: -f:把某设备指定为模拟损坏 -r:把损坏的设备移除 -a:新增一个设备到阵列中 一.创建一个大小为1G的RAID1: 对RAID1来说,其利用率只有50%,故要使RAID1的有效空间为1G,就需要两个1G的分区,其创建过程如下: [[email protected] backup]# fdisk/dev/sdc WARNING

[CareerCup] 15.7 Student Grade 学生成绩

15.7 Imagine a simple database storing information for students' grades. Design what this database might look like and provide a SQL query to return a list of the honor roll students (top 10%), sorted by their grade point average. 在一个简化的数据库中我们有三个表,St

[CareerCup] 15.5 Denormalization 逆规范化

15.5 What is denormalization? Explain the pros and cons. 逆规范化Denormalization是一种通过添加冗余数据的数据库优化技术,可以帮助我们减少关系数据库中耗时的交Join.在一般的规范化的数据库中,我们将数据存在不同的表中是为了减少冗余数据,所以我们会尝试着每条数据在数据库中只有一份. 比如说,在一个规范化的数据库中,我们有Courses表和Teachers表,每个Courses表的一项都会保存teacherID,但是没有teac

[CareerCup] 15.6 Entity Relationship Diagram 实体关系图

15.6 Draw an entity-relationship diagram for a database with companies, people, and professionals (people who work for companies). 在公司Companies工作的人是专家Proferssionals,所以人和专家是ISA("is a")的关系,每个专家有例如等级,和工作经验和其他从人Person继承来的属性.一个专家一次只能为一个公司工作,而一个公司可以雇佣

复旦大学2015--2016学年第二学期(15级)高等代数II期末考试第七大题解答

七.(本题10分) 设 $A,B,C$ 分别为 $m\times m$, $n\times n$, $m\times n$ 阶复矩阵, $M=\begin{pmatrix} A & C\\ 0 & B\\ \end{pmatrix}$ 可对角化, 求证: 矩阵方程 $AX-XB=C$ 必有解. 证明  任取 $M$ 的特征值 $\lambda_0$, $M-\lambda_0I=\begin{pmatrix} A-\lambda_0I & C \\ 0 & B-\lambd

Java并发程序设计(15)并发锁之读写锁(续二)写锁降级

1.1.1. 读写锁应用之三写锁降级 ReentrantReadWriteLock还具有写锁降级的特点,而这跟可重入性有一些关系. (1)持有写锁时可以降级为读锁. (2)持有读锁时不能升级为写锁. ReentrantReadWriteLock和ReentrantLock相似的是都有一个特点,就是可重入.可重入指已经获取到锁的线程可以再次获取锁,保证lock和unlock的次数相同即可. package com.test.concurrence; import java.util.Random;

LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2

推荐15个最好的HTML5移动模板 (二)

推荐15个最好的HTML5移动模板(一) 你创造了一个应用程序,现在你想展示给世界,那么你可以创建一个网站或登陆页面.因此,我们可以利用HTML5模板,这样就可以轻松地在互联网上公布.这些模板可以快速的提高您的业务.当你设计一个登陆页面的移动应用程序, 无论是在iPhone.Android或iPad应用程序上,你一般都只有一个目标,就是鼓励访客下载你的应用程序. 8.AVENTADOR Aventador可建立一个网站用于汽车展厅.汽车备用配件陈列室.汽车服务点和汽车护理点等同类别的网页模板,.