白嫖20道leetcode数据库题目,又是快乐的一天

今天登陆上了leetcode,发现现在新出的部分题目需要订阅才能做,我这么穷的人怎么可能订阅呢,所以就把之前做过的题目都翻出来又做了一遍.

175. 组合两个表

表1: Person

1+-------------+---------+
2| 列名         | 类型     |
3+-------------+---------+
4| PersonId    | int     |
5| FirstName   | varchar |
6| LastName    | varchar |
7+-------------+---------+

PersonId 是上表主键
表2: Address

1+-------------+---------+
2| 列名         | 类型    |
3+-------------+---------+
4| AddressId   | int     |
5| PersonId    | int     |
6| City        | varchar |
7| State       | varchar |
8+-------------+---------+
9

AddressId 是上表主键

编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:

1FirstName, LastName, City, State

解题

1select p.FirstName, p.LastName, a.City, a.State from Person p left join Address a on p.PersonId = a.PersonId

解题关键词

left join

因为表 Address 中的 personId 是表 Person 的外关键字,所以我们可以连接这两个表来获取一个人的地址信息。
考虑到可能不是每个人都有地址信息,我们应该使用 outer john 而不是默认的 inner join。

176. 第二高的薪水

编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。

1+----+--------+
2| Id | Salary |
3+----+--------+
4| 1  | 100    |
5| 2  | 200    |
6| 3  | 300    |
7+----+--------+

例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。

1+---------------------+
2| SecondHighestSalary |
3+---------------------+
4| 200                 |
5+---------------------+

解题

1# Write your MySQL query statement below
2select ifnull(
3    (select Salary from Employee group by Salary order by Salary desc limit 1,1),null) SecondHighestSalary

解题关键字

ifnull

 1IFNULL(expr1,expr2) 
 2如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2。IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境。 
 3mysql> select IFNULL(1,0);
 4        -> 1
 5mysql> select IFNULL(0,10);
 6        -> 0
 7mysql> select IFNULL(1/0,10);
 8        -> 10
 9mysql> select IFNULL(1/0,'yes');
10        -> 'yes'
11
12IF(expr1,expr2,expr3) 
13如果expr1是TRUE(expr1<>0且expr1<>NULL),那么IF()返回expr2,否则它返回expr3。IF()返回一个数字或字符串值,取决于它被使用的上下文。 
14mysql> select IF(1>2,2,3);
15        -> 3
16mysql> select IF(1<2,'yes','no');
17        -> 'yes'
18mysql> select IF(strcmp('test','test1'),'yes','no');
19        -> 'no'
20
21expr1作为整数值被计算,它意味着如果你正在测试浮点或字符串值,你应该使用一个比较操作来做。
22
23mysql> select IF(0.1,1,0);
24        -> 0
25mysql> select IF(0.1<>0,1,0);
26        -> 1
27
28在上面的第一种情况中,IF(0.1)返回0,因为0.1被变换到整数值, 导致测试IF(0)。这可能不是你期望的。在第二种情况中,比较测试原来的浮点值看它是否是非零,比较的结果被用作一个整数。

177. 第N高的薪水

编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。

1+----+--------+
2| Id | Salary |
3+----+--------+
4| 1  | 100    |
5| 2  | 200    |
6| 3  | 300    |
7+----+--------+

例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水,那么查询应返回 null。

1+------------------------+
2| getNthHighestSalary(2) |
3+------------------------+
4| 200                    |
5+------------------------+

解题

 1CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
 2BEGIN
 3  Declare M INT;
 4
 5  Set M = N-1;
 6  RETURN (
 7      # Write your MySQL query statement below.
 8      SELECT Salary from Employee group by Salary order by Salary desc limit M,1
 9  );
10END

178. 分数排名

编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。

 1+----+-------+
 2| Id | Score |
 3+----+-------+
 4| 1  | 3.50  |
 5| 2  | 3.65  |
 6| 3  | 4.00  |
 7| 4  | 3.85  |
 8| 5  | 4.00  |
 9| 6  | 3.65  |
10+----+-------+
11

例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):

 1+-------+------+
 2| Score | Rank |
 3+-------+------+
 4| 4.00  | 1    |
 5| 4.00  | 1    |
 6| 3.85  | 2    |
 7| 3.65  | 3    |
 8| 3.65  | 3    |
 9| 3.50  | 4    |
10+-------+------+
11

解题思路

按照分数排序,生成查询后排名序号

1select s.Score,(select count(distinct Score) from Scores where Score >=s.Score) as Rank from Scores as s order by s.Score desc 

distinct

去重函数 可以理解为 group by score

180. 连续出现的数字

编写一个 SQL 查询,查找所有至少连续出现三次的数字。

 1+----+-----+
 2| Id | Num |
 3+----+-----+
 4| 1  |  1  |
 5| 2  |  1  |
 6| 3  |  1  |
 7| 4  |  2  |
 8| 5  |  1  |
 9| 6  |  2  |
10| 7  |  2  |
11+----+-----+

例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。

1+-----------------+
2| ConsecutiveNums |
3+-----------------+
4| 1               |
5+-----------------+

解题

1select distinct l1.Num ConsecutiveNums from Logs l1
2left join Logs l2 on l1.Id = l2.Id - 1
3left join Logs l3 on l1.Id = l3.Id - 2
4where l1.Num = l2.Num and l2.Num = l3.Num;

181. 超过经理收入的员工

Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。

1+----+-------+--------+-----------+
2| Id | Name  | Salary | ManagerId |
3+----+-------+--------+-----------+
4| 1  | Joe   | 70000  | 3         |
5| 2  | Henry | 80000  | 4         |
6| 3  | Sam   | 60000  | NULL      |
7| 4  | Max   | 90000  | NULL      |
8+----+-------+--------+-----------+
9

给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。

1+----------+
2| Employee |
3+----------+
4| Joe      |
5+----------+

解题思路

需要一个sql查询该表两次 且where条件会有两次查询后的薪资判断

1Select e1.Name as Employee from Employee as e1 , Employee as e2  where e1.ManagerId = e2.Id and e1.Salary > e2.Salary
2
3or
4
5Select e1.Name as Employee from Employee e1 join Employee e2  on e1.ManagerId = e2.Id and e1.Salary > e2.Salary

182. 查找重复的电子邮箱

编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。

示例:

1+----+---------+
2| Id | Email   |
3+----+---------+
4| 1  | a@b.com |
5| 2  | c@d.com |
6| 3  | a@b.com |
7+----+---------+

根据以上输入,你的查询应返回以下结果:

1+---------+
2| Email   |
3+---------+
4| a@b.com |
5+---------+

说明:所有电子邮箱都是小写字母。

解题

1select Email from Person group by Email having count(Id) >1

183. 从不订购的客户

某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

Customers 表:

1+----+-------+
2| Id | Name  |
3+----+-------+
4| 1  | Joe   |
5| 2  | Henry |
6| 3  | Sam   |
7| 4  | Max   |
8+----+-------+
9

Orders 表:

1+----+------------+
2| Id | CustomerId |
3+----+------------+
4| 1  | 3          |
5| 2  | 1          |
6+----+------------+
7

例如给定上述表格,你的查询应返回:

1+-----------+
2| Customers |
3+-----------+
4| Henry     |
5| Max       |
6+-----------+

解题

1select Name Customers from Customers where Id not in (select CustomerId from Orders)

184. 部门工资最高的员工

Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。

1+----+-------+--------+--------------+
2| Id | Name  | Salary | DepartmentId |
3+----+-------+--------+--------------+
4| 1  | Joe   | 70000  | 1            |
5| 2  | Henry | 80000  | 2            |
6| 3  | Sam   | 60000  | 2            |
7| 4  | Max   | 90000  | 1            |
8+----+-------+--------+--------------+
9

Department 表包含公司所有部门的信息。

1+----+----------+
2| Id | Name     |
3+----+----------+
4| 1  | IT       |
5| 2  | Sales    |
6+----+----------+
7

编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。

1+------------+----------+--------+
2| Department | Employee | Salary |
3+------------+----------+--------+
4| IT         | Max      | 90000  |
5| Sales      | Henry    | 80000  |
6+------------+----------+--------+

解题

1select d.Name Department,e.Name Employee, e.Salary from Department d inner join Employee e on d.Id = e.DepartmentId and e.Salary = (select max(Salary) from Employee where DepartmentId = d.Id)

185. 部门工资前三高的所有员工

Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id 。

 1+----+-------+--------+--------------+
 2| Id | Name  | Salary | DepartmentId |
 3+----+-------+--------+--------------+
 4| 1  | Joe   | 70000  | 1            |
 5| 2  | Henry | 80000  | 2            |
 6| 3  | Sam   | 60000  | 2            |
 7| 4  | Max   | 90000  | 1            |
 8| 5  | Janet | 69000  | 1            |
 9| 6  | Randy | 85000  | 1            |
10+----+-------+--------+--------------+
11

Department 表包含公司所有部门的信息。

1+----+----------+
2| Id | Name     |
3+----+----------+
4| 1  | IT       |
5| 2  | Sales    |
6+----+----------+
7

编写一个 SQL 查询,找出每个部门工资前三高的员工。例如,根据上述给定的表格,查询结果应返回:

1+------------+----------+--------+
2| Department | Employee | Salary |
3+------------+----------+--------+
4| IT         | Max      | 90000  |
5| IT         | Randy    | 85000  |
6| IT         | Joe      | 70000  |
7| Sales      | Henry    | 80000  |
8| Sales      | Sam      | 60000  |
9+------------+----------+--------+

解题

1SELECT D1.Name Department,E1.Name Employee,E1.Salary
2FROM Employee E1, Employee E2, Department D1
3WHERE E1.DepartmentID = E2.DepartmentID
4AND E2.Salary >= E1.Salary 
5AND E1.DepartmentID = D1.ID      
6GROUP BY E1.Name
7HAVING COUNT(DISTINCT E2.Salary) <= 3
8ORDER BY D1.Name, E1.Salary DESC;

196. 删除重复的电子邮箱

编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。

1+----+------------------+
2| Id | Email            |
3+----+------------------+
4| 1  | john@example.com |
5| 2  | bob@example.com  |
6| 3  | john@example.com |
7+----+------------------+

Id 是这个表的主键。
例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:

1+----+------------------+
2| Id | Email            |
3+----+------------------+
4| 1  | john@example.com |
5| 2  | bob@example.com  |
6+----+------------------+
7

解题

1delete p2 FROM Person p1 JOIN Person p2
2ON p2.Email = p1.Email WHERE p2.Id > p1.Id;

197. 上升的温度

给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。

1+---------+------------------+------------------+
2| Id(INT) | RecordDate(DATE) | Temperature(INT) |
3+---------+------------------+------------------+
4|       1 |       2015-01-01 |               10 |
5|       2 |       2015-01-02 |               25 |
6|       3 |       2015-01-03 |               20 |
7|       4 |       2015-01-04 |               30 |
8+---------+------------------+------------------+

例如,根据上述给定的 Weather 表格,返回如下 Id:

1+----+
2| Id |
3+----+
4|  2 |
5|  4 |
6+----+

解题

1select w1.Id from Weather w1 , Weather w2
2where  w1.RecordDate = DATE_ADD(w2.RecordDate,INTERVAL 1 DAY )
3and w1.Temperature > w2.Temperature

262. 行程和用户

Trips 表中存所有出租车的行程信息。每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键。Status 是枚举类型,枚举成员为 (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’)。

 1+----+-----------+-----------+---------+--------------------+----------+
 2| Id | Client_Id | Driver_Id | City_Id |        Status      |Request_at|
 3+----+-----------+-----------+---------+--------------------+----------+
 4| 1  |     1     |    10     |    1    |     completed      |2013-10-01|
 5| 2  |     2     |    11     |    1    | cancelled_by_driver|2013-10-01|
 6| 3  |     3     |    12     |    6    |     completed      |2013-10-01|
 7| 4  |     4     |    13     |    6    | cancelled_by_client|2013-10-01|
 8| 5  |     1     |    10     |    1    |     completed      |2013-10-02|
 9| 6  |     2     |    11     |    6    |     completed      |2013-10-02|
10| 7  |     3     |    12     |    6    |     completed      |2013-10-02|
11| 8  |     2     |    12     |    12   |     completed      |2013-10-03|
12| 9  |     3     |    10     |    12   |     completed      |2013-10-03| 
13| 10 |     4     |    13     |    12   | cancelled_by_driver|2013-10-03|
14+----+-----------+-----------+---------+--------------------+----------+
15

Users 表存所有用户。每个用户有唯一键 Users_Id。Banned 表示这个用户是否被禁止,Role 则是一个表示(‘client’, ‘driver’, ‘partner’)的枚举类型。

 1+----------+--------+--------+
 2| Users_Id | Banned |  Role  |
 3+----------+--------+--------+
 4|    1     |   No   | client |
 5|    2     |   Yes  | client |
 6|    3     |   No   | client |
 7|    4     |   No   | client |
 8|    10    |   No   | driver |
 9|    11    |   No   | driver |
10|    12    |   No   | driver |
11|    13    |   No   | driver |
12+----------+--------+--------+

写一段 SQL 语句查出 2013年10月1日 至 2013年10月3日 期间非禁止用户的取消率。基于上表,你的 SQL 语句应返回如下结果,取消率(Cancellation Rate)保留两位小数。

1+------------+-------------------+
2|     Day    | Cancellation Rate |
3+------------+-------------------+
4| 2013-10-01 |       0.33        |
5| 2013-10-02 |       0.00        |
6| 2013-10-03 |       0.50        |
7+------------+-------------------+

解题

1select t.Request_at Day,ROUND(sum((case when t.Status like 'cancelled%' then 1 else 0 end))/count(*),2) as'Cancellation Rate'
2from Trips t
3inner join Users u on u.Users_Id =t.Client_Id and u.Banned = 'No'
4where t.Request_at between '2013-10-01'and'2013-10-03' group by t.Request_at;

595. 大的国家

这里有张 World

1+-----------------+------------+------------+--------------+---------------+
2| name            | continent  | area       | population   | gdp           |
3+-----------------+------------+------------+--------------+---------------+
4| Afghanistan     | Asia       | 652230     | 25500100     | 20343000      |
5| Albania         | Europe     | 28748      | 2831741      | 12960000      |
6| Algeria         | Africa     | 2381741    | 37100000     | 188681000     |
7| Andorra         | Europe     | 468        | 78115        | 3712000       |
8| Angola          | Africa     | 1246700    | 20609294     | 100990000     |
9+-----------------+------------+------------+--------------+---------------+

如果一个国家的面积超过300万平方公里,或者人口超过2500万,那么这个国家就是大国家。

编写一个SQL查询,输出表中所有大国家的名称、人口和面积。

例如,根据上表,我们应该输出:

1+--------------+-------------+--------------+
2| name         | population  | area         |
3+--------------+-------------+--------------+
4| Afghanistan  | 25500100    | 652230       |
5| Algeria      | 37100000    | 2381741      |
6+--------------+-------------+--------------+

题解

1select name,population,area from World where area>3000000 or population>25000000

596. 超过5名学生的课

有一个courses 表 ,有: student (学生) 和 class (课程)。

请列出所有超过或等于5名学生的课。

例如,表:

 1+---------+------------+
 2| student | class      |
 3+---------+------------+
 4| A       | Math       |
 5| B       | English    |
 6| C       | Math       |
 7| D       | Biology    |
 8| E       | Math       |
 9| F       | Computer   |
10| G       | Math       |
11| H       | Math       |
12| I       | Math       |
13+---------+------------+
14

应该输出:

1+---------+
2| class   |
3+---------+
4| Math    |
5+---------+

Note:

学生在每个课中不应被重复计算。


题解

1select class from courses group by class having count(DISTINCT student) >= 5

601. 体育馆的人流量

X 市建了一个新的体育馆,每日人流量信息被记录在这三列信息中:序号 (id)、日期 (date)、 人流量 (people)。

请编写一个查询语句,找出高峰期时段,要求连续三天及以上,并且每天人流量均不少于100。

例如,表 stadium

 1+------+------------+-----------+
 2| id   | date       | people    |
 3+------+------------+-----------+
 4| 1    | 2017-01-01 | 10        |
 5| 2    | 2017-01-02 | 109       |
 6| 3    | 2017-01-03 | 150       |
 7| 4    | 2017-01-04 | 99        |
 8| 5    | 2017-01-05 | 145       |
 9| 6    | 2017-01-06 | 1455      |
10| 7    | 2017-01-07 | 199       |
11| 8    | 2017-01-08 | 188       |
12+------+------------+-----------+

对于上面的示例数据,输出为:

1+------+------------+-----------+
2| id   | date       | people    |
3+------+------------+-----------+
4| 5    | 2017-01-05 | 145       |
5| 6    | 2017-01-06 | 1455      |
6| 7    | 2017-01-07 | 199       |
7| 8    | 2017-01-08 | 188       |
8+------+------------+-----------+

Note:

每天只有一行记录,日期随着 id 的增加而增加。


题解

 1select distinct s1.* from stadium s1, stadium s2, stadium s3
 2where s1.people >= 100 and s2.people>= 100 and s3.people >= 100
 3and
 4(
 5    (s1.id - s2.id = 1 and s2.id - s3.id =1)
 6   or
 7   (s2.id - s1.id = 1 and s1.id - s3.id =1) 
 8   or
 9   (s3.id - s2.id = 1 and s2.id - s1.id = 1) 
10) order by s1.id;

620. 有趣的电影

*leetcode

某城市开了一家新的电影院,吸引了很多人过来看电影。该电影院特别注意用户体验,专门有个 LED显示板做电影推荐,上面公布着影评和相关电影描述。

作为该电影院的信息部主管,您需要编写一个 SQL查询,找出所有影片描述为非 boring (不无聊) 的并且 id 为奇数 的影片,结果请按等级 rating 排列。

例如,下表 cinema:

1+---------+-----------+--------------+-----------+
2|   id    | movie     |  description |  rating   |
3+---------+-----------+--------------+-----------+
4|   1     | War       |   great 3D   |   8.9     |
5|   2     | Science   |   fiction    |   8.5     |
6|   3     | irish     |   boring     |   6.2     |
7|   4     | Ice song  |   Fantacy    |   8.6     |
8|   5     | House card|   Interesting|   9.1     |
9+---------+-----------+--------------+-----------+

对于上面的例子,则正确的输出是为:

1+---------+-----------+--------------+-----------+
2|   id    | movie     |  description |  rating   |
3+---------+-----------+--------------+-----------+
4|   5     | House card|   Interesting|   9.1     |
5|   1     | War       |   great 3D   |   8.9     |
6+---------+-----------+--------------+-----------+
7

题解

1select * from cinema where description!="boring" and id%2=1 order by rating desc 

626. 换座位

小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。

其中纵列的 id 是连续递增的

小美想改变相邻俩学生的座位。

你能不能帮她写一个 SQL query 来输出小美想要的结果呢?

示例:

1+---------+---------+
2|    id   | student |
3+---------+---------+
4|    1    | Abbot   |
5|    2    | Doris   |
6|    3    | Emerson |
7|    4    | Green   |
8|    5    | Jeames  |
9+---------+---------+

假如数据输入的是上表,则输出结果如下:

1+---------+---------+
2|    id   | student |
3+---------+---------+
4|    1    | Doris   |
5|    2    | Abbot   |
6|    3    | Green   |
7|    4    | Emerson |
8|    5    | Jeames  |
9+---------+---------+

注意:

如果学生人数是奇数,则不需要改变最后一个同学的座位。

题解

1select * from (
2    select s1.id, s2.student from seat s1,seat s2 where s1.id%2=0 and s1.id-1=s2.id
3    union
4    select s1.id, s2.student from seat s1,seat s2 where s1.id%2=1 and s1.id=s2.id-1
5    union
6    select id, student from seat s1 where id%2=1 and id=(select id from seat order by id desc limit 1)
7) as t order by t.id

解题过程

1奇数座位&偶数座位的学生姓名
2偶数座位&奇数座位的学生姓名
3如果表中的数据是奇数则最后一个会漏掉
4所以在查询一下id为最后一个且是奇数座位

627. 交换工资

给定一个 salary表,如下所示,有m=男性 和 f=女性的值 。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求使用一个更新查询,并且没有中间临时表。

例如:

idnamesexsalary
1Am2500
2Bf1500
3Cm5500
4Df500

运行你所编写的查询语句之后,将会得到以下表:

idnamesexsalary
1Af2500
2Bm1500
3Cf5500
4Dm500

解题

1update salary set sex= case sex
2when 'm' then 'f'
3else 'm'
4end;
5or
6update salary set sex=IF(sex='m', 'f', 'm')

解题思路

1上面给的实例是交换性别
2所以按照交换性别的思路就行

1179. 重新格式化部门表

部门表 Department:

1+---------------+---------+
2| Column Name   | Type    |
3+---------------+---------+
4| id            | int     |
5| revenue       | int     |
6| month         | varchar |
7+---------------+---------+

(id, month) 是表的联合主键。
这个表格有关于每个部门每月收入的信息。
月份(month)可以取下列值 ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]。

编写一个 SQL 查询来重新格式化表,使得新的表中有一个部门 id 列和一些对应 每个月 的收入(revenue)列。

查询结果格式如下面的示例所示:

Department 表:

 1shell script
 2+------+---------+-------+
 3| id   | revenue | month |
 4+------+---------+-------+
 5| 1    | 8000    | Jan   |
 6| 2    | 9000    | Jan   |
 7| 3    | 10000   | Feb   |
 8| 1    | 7000    | Feb   |
 9| 1    | 6000    | Mar   |
10+------+---------+-------+

查询得到的结果表:

1shell script
2+------+-------------+-------------+-------------+-----+-------------+
3| id   | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue |
4+------+-------------+-------------+-------------+-----+-------------+
5| 1    | 8000        | 7000        | 6000        | ... | null        |
6| 2    | 9000        | null        | null        | ... | null        |
7| 3    | null        | 10000       | null        | ... | null        |
8+------+-------------+-------------+-------------+-----+-------------+

注意,结果表有 13 列 (1个部门 id 列 + 12个月份的收入列)。

解题

 1shell script
 2select 
 3     id
 4    , sum(case `month` when 'Jan' then revenue else null end) as Jan_Revenue
 5    , sum(case `month` when 'Feb' then revenue else null end) as Feb_Revenue
 6    , sum(case `month` when 'Mar' then revenue else null end) as Mar_Revenue
 7    , sum(case `month` when 'Apr' then revenue else null end) as Apr_Revenue
 8    , sum(case `month` when 'May' then revenue else null end) as May_Revenue
 9    , sum(case `month` when 'Jun' then revenue else null end) as Jun_Revenue
10    , sum(case `month` when 'Jul' then revenue else null end) as Jul_Revenue
11    , sum(case `month` when 'Aug' then revenue else null end) as Aug_Revenue
12    , sum(case `month` when 'Sep' then revenue else null end) as Sep_Revenue
13    , sum(case `month` when 'Oct' then revenue else null end) as Oct_Revenue
14    , sum(case `month` when 'Nov' then revenue else null end) as Nov_Revenue
15    , sum(case `month` when 'Dec' then revenue else null end) as Dec_Revenue
16from Department group by id

后话

创作不易,希望对你有所帮助。
如果本篇博客有任何错误,请批评指教,不胜感激!!!

原创不易,转载请注明处。
文章将持续更新中,可以通过微信搜索[石先生的私房菜]或者下方二维码关注第一时间阅读和催更,除了博客以外还会定期发送leetcodephp版题解

20200619155130

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值