codewars--两张表连接查询的操作
2017-11-01 12:03
176 查看
题目:For this challenge you need to create a simple SELECT statement that will return all columns from the
and join to the
sale_count.
id
name
id
people_id
sale
price
You should return all people fields as well as the sale count as "sale_count" and the rank as "sale_rank".
NOTE: You're solution should use pure SQL. Ruby is used within the test cases to do the actual testing.
解决方案:
SELECT
p.*,
COUNT(s)
as sale_count,
RANK()
OVER (ORDER
BY sum(s.price)
DESC) as sale_rank
FROM people p
JOIN sales s
ON s.people_id = p.id
GROUP
BY p.id
peopletable,
and join to the
salestable so that you can return the COUNT of all sales and RANK each person by their
sale_count.
people table schema
idname
sales table schema
idpeople_id
sale
price
You should return all people fields as well as the sale count as "sale_count" and the rank as "sale_rank".
NOTE: You're solution should use pure SQL. Ruby is used within the test cases to do the actual testing.
解决方案:
SELECT
p.*,
COUNT(s)
as sale_count,
RANK()
OVER (ORDER
BY sum(s.price)
DESC) as sale_rank
FROM people p
JOIN sales s
ON s.people_id = p.id
GROUP
BY p.id
相关文章推荐
- 学习python之编写简单简单连接数据库并执行查询操作
- 两张图解读Oracle 数据库的 union all、union、intersect、minus以及各种连接查询。
- SQL操作,连接查询_左连接/右连接/全连接的区别 http://www.sqlzoo.cn/
- SQL左连接——查询每个用户各操作类型的文章数量
- SQL语句,多表查询,连接查询,嵌套查询,集合操作,有IN谓词,有比较运算符,有ANY\EXIST谓词的查询
- ORACLE查询当前连接的用户信息及操作的SQL语句
- 在SQL Server 2005中连接Oracle,完成查询、插入操作
- C#使用oledb连接excel运行Insert Into语句出现“操作必须使用一个可更新的查询”的解决的方法
- 如何用一张JSP页面连接数据库,实现查询、修改操作
- go 语言使用ssh包连接操作Linux服务器,使用ping包查看服务器网络是否正常,使用cron包定时查询该服务器CPU使用情况
- oracle 查询操作用的相关信息 当前操作人 操作人主机名称 主机ip 连接oracle 外网ip
- 连接oracle11g数据库的jdbc的查询操作
- SQL 连接操作 及 查询分析
- 利用dbcp数据库连接池连接数据库,并利用dbtuils框架对数据库test中book表进行插入、删除、修改、查询的操作。book表结构如下:(id,name,price,author,descrip
- 关于怎么C#控制台窗口中怎么创建连接查询数据库操作
- 在SQL Server 2005中连接Oracle,完成查询、插入操作
- VS2010连接SQL Server 2008并执行查询操作
- C/C++连接MySQL数据库和查询操作案例!!
- PHP入门教程之使用Mysqli操作数据库的方法(连接,查询,事务回滚等)
- Asp.Net连接Oracle数据库的通用类orahelper.cs并且利用它对数据库查询、汇总、更新等操作演示