如何查询SQL案例语句中的许多情况
我正在尝试解决SQL在线挑战,
我有三张表:
- 销售:customer_id,order_date,product_id。
- 成员:join_date,member_id。
- 菜单:product_id,product_name,价格。
问题之一是:每个成员成为成员之前的总项目和金额是多少?
我想我在以下查询中得到了写作答案:
with cte as
(
SELECT
CASE WHEN s.customer_id = 'A' THEN count(s.product_id)*m.price END AS purchases_A,
CASE WHEN s.customer_id = 'B' THEN count(s.product_id)*m.price END AS purchases_B,
CASE WHEN s.customer_id = 'C' THEN count(s.product_id)*m.price END AS purchases_C,
case when s.customer_id = 'A' THEN count(s.product_id) END AS total_A,
case when s.customer_id = 'B' THEN count(s.product_id) END AS total_B,
case when s.customer_id = 'C' THEN count(s.product_id) END AS total_C
from sales s
join menu m on s.product_id = m.product_id
join members mb on mb.customer_id = s.customer_id and mb.join_date > s.order_date
group by s.customer_id, m.price
)
select
sum(purchases_A) as total_spendings_a,
sum (total_A) as total_items_A,
sum(purchases_B) as total_spendings_b,
sum (total_B) as total_items_B,
sum(purchases_C) as total_spendings_c,
sum (total_C) as total_items_C
from cte;
我的问题是。编写此查询时,有更好的方法或更有效的方法吗?感觉太长又重复。 在这种情况下,我只有三个客户:A,B和C 如果我有100或1000个客户怎么办?
I am trying to solve an SQL online-challenge
I have three tables:
- sales: customer_id, order_date, product_id.
- members: join_date, member_id.
- menu: product_id, product_name, price.
and one of the questions is: What is the total items and amount spent for each member before they became a member?
I think I got the write answer right the following query:
with cte as
(
SELECT
CASE WHEN s.customer_id = 'A' THEN count(s.product_id)*m.price END AS purchases_A,
CASE WHEN s.customer_id = 'B' THEN count(s.product_id)*m.price END AS purchases_B,
CASE WHEN s.customer_id = 'C' THEN count(s.product_id)*m.price END AS purchases_C,
case when s.customer_id = 'A' THEN count(s.product_id) END AS total_A,
case when s.customer_id = 'B' THEN count(s.product_id) END AS total_B,
case when s.customer_id = 'C' THEN count(s.product_id) END AS total_C
from sales s
join menu m on s.product_id = m.product_id
join members mb on mb.customer_id = s.customer_id and mb.join_date > s.order_date
group by s.customer_id, m.price
)
select
sum(purchases_A) as total_spendings_a,
sum (total_A) as total_items_A,
sum(purchases_B) as total_spendings_b,
sum (total_B) as total_items_B,
sum(purchases_C) as total_spendings_c,
sum (total_C) as total_items_C
from cte;
And my question is. is there a better way or more efficient way in writing this query? it feels too long and repetitive.
In this case I had only three customers: A,B, and C
what if I have 100 or 1000 customers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需加入表格,然后按成员汇总:
您提到
member.member_id
and成员。如果这是您请求中的错别字,则只需将错误的列名更改为我查询中的正确列名即可。
Simply join the tables and aggregate by member:
You mention both
members.member_id
andmembers.customer_id
, so I've used them both. If this was a typo in your request, then just change the wrong column name to the correct one in my query.