开发者

Query for a video rental database which I cant seem to figure out?

开发者 https://www.devze.com 2023-02-14 21:53 出处:网络
I have a query which i have been working on for hours and can\'t seem to get it working. the query is to Generate list of customers that have spent>€100 in the last 365 days. I am creating a video开发

I have a query which i have been working on for hours and can't seem to get it working. the query is to Generate list of customers that have spent > €100 in the last 365 days. I am creating a video开发者_Python百科 rental database..

This is how far i have gotten but cant seem to link the data together with date_rented data table.

SELECT CUST_ID, CUSTOMER_SPEND
FROM ACCOUNT_TEST
WHERE CUSTOMER_SPEND > 100;

the tables I am working with are cust_id, customer_spend, date_rented and account_test


Instead of trying to answer the question that was (sort of) asked, maybe it makes sense to step back, look at the (apparently) desired result, and show how that could be achieved. For the moment, I'm going to only look at one table out of what should be a number. This table will hold the details of an individual customer rental:

customer_id
date_rented
cost

More fields are certainly needed, but those seem to cover what we care about for this query. From this, we want a list of customers who've spent at least 100 (of whatever unit cost is in), along with the amount spent by each. The only slightly tricky part is that we can't use an aggregate like sum(customer_paid) in a where clause, so we put that in a having clause instead.

select customer_id, sum(cost) as customer_paid
    from rental_details
    where to_days(now()) - to_days(date_rented) <= 365
    group by customer_id
    having customer_paid > 100

As a quick warning, that might need minor tweaking to work with MySQL -- most of what I've written recently has been for SQL Server.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号