题目: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
本文介绍了一种使用纯SQL的方法来从people表中查询所有字段,并通过连接到sales表来获取每个人的销售总数及排名。解决方案包括了SELECT语句、JOIN操作及聚合函数COUNT和RANK的使用。
1320

被折叠的 条评论
为什么被折叠?



