SQL查询最后租用的具有天窗功能的汽车在汽车租赁系统中。

huangapple go评论57阅读模式
英文:

SQL query for last rented car with sunroof feature in car rental system

问题

I'm developing a car rental automation system in SQL, and I'm seeing unwanted data in my table that I've received and constantly updated. The query I need to write is as follows: 'Write a query that retrieves information about the last rented car for customers who have rented cars with the feature of a sunroof at least once.' I've been struggling with it for hours and couldn't solve it. Can you help me?

select * from car a
where exists(
    select * from car a2
    inner join customer m on m.customer_id=a2.customer_id
    inner join rent k on k.customer_id=m.customer_id
    inner join car_rent ak on ak.rent_id=k.rent_id
    inner join package_package_option pk on pk.package_id=a2.package_id
    where pk.option_id=2 and a.rent_sell=1
    group by ak.date
    having ak.date = max(ak.date)
)
英文:

I'm developing a car rental automation system in SQL, and I'm seeing unwanted data in my table that I've received and constantly updated. The query I need to write is as follows: 'Write a query that retrieves information about the last rented car for customers who have rented cars with the feature of a sunroof at least once.' I've been struggling with it for hours and couldn't solve it. Can you help me?

select * from car a
where exists(
    select * from car a2
    inner join customer m on m.customer_id=a2.customer_id
    inner join rent k on k.customer_id=m.customer_id
    inner join car_rent ak on ak.rent_id=k.rent_id
    inner join package_package_option pk on pk.package_id=a2.package_id
    where pk.option_id=2 and a.rent_sell=1
    group by ak.date
    having ak.date = max(ak.date)

答案1

得分: 1

解决方案分为两个步骤:

  1. 识别所有曾租用过带天窗汽车的客户,以及
  2. 对于每位这样的客户,查找每位客户的最新租车记录。

第一步相对简单 - 过滤具有天窗的租车并选择不同的客户ID。

第二步可以有几种方式完成。其中一种方式是将客户ID输入到一个CROSS APPLY (SELECT TOP 1 ... ORDER BY RentalDate DESC) 结构中,逐个选择每个选定客户的最新租车记录。另一种方式是查找所选客户的所有租车记录,使用ROW_NUMBER() 窗口函数 OVER(... ORDER BY RentalDate DESC) 分配序列号,然后筛选出行号 = 1。

类似于(伪代码):

SELECT *
FROM (
    SELECT DISTINCT customer_id
    FROM rentals
    WHERE has-a-sunroof
    AND is-a-rental
) C
CROSS APPLY (
    SELECT TOP 1 rental-info
    FROM rentals R
    WHERE R.customer_id = C.customer_id
    AND is-a-rental
    ORDER BY R.Rentaldate DESC
) R

或者

SELECT *
FROM (
    SELECT rental-info,
        ROW_NUMBER() OVER(PARTITION BY R.customer_id ORDER BY R.Rentaldate DESC) RowNum
    FROM rentals R
    WHERE R.customer_id IN (
        SELECT DISTINCT customer_id
        FROM rentals
        WHERE has-a-sunroof
        AND is-a-rental
    )
    AND is-a-rental
) A
WHERE A.RowNum = 1

您可以尝试这两种方式,以比较在大型数据集上的性能。我建议还要确保在租车表上建立一个索引,包括customer_id和rental date以获得最佳性能。

英文:

The solution has two steps:

  1. Identify all customers who have ever rented a car with a sunroof, and
  2. For each such customer, look up the latest rental for each such customer.

The first step is pretty straight forward - Filter the rentals for sunroof and select distinct customer IDs.

The second step can be done a couple of ways. One is to feed the customer IDs into a CROSS APPLY (SELECT TOP 1 ... ORDER BY RentalDate DESC) construct to select the latest rental for each selected customer one at a time. Another is to lookup all rentals for the selected customers, assign sequence numbers using the ROW_NUMBER() window function OVER(... ORDER BY RentalDate DESC), and then filtering for row-number = 1.

Something like (pseudocode):

SELECT *
FROM (
    SELECT DISTINCT customer_id
    FROM rentals
    WHERE has-a-sunroof
    AND is-a-rental
) C
CROSS APPLY (
    SELECT TOP 1 rental-info
    FROM rentals R
    WHERE R.customer_id = C.customer_id
    AND is-a-rental
    ORDER BY R.Rentaldate DESC
) R

or

SELECT *
FROM (
    SELECT rental-info,
        ROW_NUMBER() OVER(PARTITION BY R.customer_id ORDER BY R.Rentaldate DESC) RowNum
    FROM rentals R
    WHERE R.customer_id IN (
        SELECT DISTINCT customer_id
        FROM rentals
        WHERE has-a-sunroof
        AND is-a-rental
    )
    AND is-a-rental
) A
WHERE A.RowNum = 1

You might try both to compare performance with large data sets. I recommend also ensuring that you have an index on the rental that includes both customer_id and rental date for best performance.

huangapple
  • 本文由 发表于 2023年6月2日 00:20:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定