SQL:为第n个客户查找第n个订单

25岁

我对SQL还是很陌生,已经学习了大约3个星期,并且喜欢它。希望在开始申请Data Analyst角色之前先提高自己的技能。

我一直在使用虚拟DVD出租数据库,但发现自己无法解决同龄人给我的挑战。问题是:“第四位客户最昂贵的租金是多少?”

在此处输入图片说明

We can see in picture, that based on the nth_customer column, Terrance Roush is the 4th ever customer (he's the 4th ever person to pay). But the issue is that the nth_customer column is actually reporting back the nth order and continues counting to infinity. So the next time Terrance shows up, the nth_customer column will not show '4' (which is what I was hoping to achieve).

Would appreciate any feedback on how to solve this. Thank you in advance.

Blue Star

If "the fourth customer" means the customer who did the fourth rental, you can break the problem down into two - finding that fourth customer, and finding their most expensive rental. Something like this:

SELECT * 
FROM payment 
WHERE customer_id = (
    SELECT customer_id 
    FROM payment 
    ORDER BY payment_date 
    LIMIT 1 OFFSET 3
) 
ORDER BY amount DESC
LIMIT 1;

在这里,我在子查询中找到了第四个客户的ID,使用LIMIT&OFFSET来获取我想要的一个记录。然后在外部查询中,我只是对所有该客户的记录进行排序,并选择数量最大的记录。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

查找数字的第n个根的算法

来自分类Dev

BeautifulSoup:查找元素的第n个出现

来自分类Dev

BeautifulSoup:查找元素的第n个出现

来自分类Dev

R : 查找第 n 个重复项

来自分类Dev

在一个很长的sql文件中查找第n个查询

来自分类Dev

在一个很长的sql文件中查找第n个查询

来自分类Dev

第n个孩子的行为像第n个孩子

来自分类Dev

查找并行C / C ++的第n个置换

来自分类Dev

查找字母(列表)的第n个组合(增量方法)

来自分类Dev

PHP:Simple Dom Parser查找第N个元素类

来自分类Dev

查找字母(列表)的第n个组合(增量方法)

来自分类Dev

使用Selenium Webdriver按类查找第n个元素

来自分类Dev

使用Eratosthens的Seive查找第n个素数

来自分类Dev

Excel查找多个条件的第N个实例

来自分类Dev

Cantor的ZigZag函数:查找第n个单元格

来自分类Dev

查找并行C / C ++的第n个置换

来自分类Dev

在原位查找第n个最大元素

来自分类Dev

Racket,编写查找列表中第 n 个元素的函数

来自分类Dev

在 Excel 列表中查找第 n 个匹配项

来自分类Dev

标记第n个刻度

来自分类Dev

BigInteger的第N个根

来自分类Dev

使用第n个类型

来自分类Dev

停在第n个元素?

来自分类Dev

列表的第n个元素

来自分类Dev

寻找第n个素数

来自分类Dev

使用第n个类型

来自分类Dev

CSS第n个选择

来自分类Dev

计算第 n 个阶乘

来自分类Dev

第 n 个孩子的食谱