您的位置:首页 > 其它

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
people
table,
and join to the
sales
table so that you can return the COUNT of all sales and RANK each person by their
sale_count.


people table schema

id
name


sales table schema

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐