如何查询SQL案例语句中的许多情况

发布于 2025-02-02 05:53:01 字数 1271 浏览 1 评论 0原文

我正在尝试解决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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2025-02-09 05:53:01

只需加入表格,然后按成员汇总:

select
  m.member_id,
  coalesce(count(*), 0) as total_amount,
  coalesce(sum(price), 0) as total_price
from members m
left join sales s on s.customer_id = m.customer_id and s.order_date < m.join_date
left join menu p on p.product_id = s.product_id
group by m.member_id
order by by m.member_id;

您提到member.member_id and 成员。如果这是您请求中的错别字,则只需将错误的列名更改为我查询中的正确列名即可。

Simply join the tables and aggregate by member:

select
  m.member_id,
  coalesce(count(*), 0) as total_amount,
  coalesce(sum(price), 0) as total_price
from members m
left join sales s on s.customer_id = m.customer_id and s.order_date < m.join_date
left join menu p on p.product_id = s.product_id
group by m.member_id
order by by m.member_id;

You mention both members.member_id and members.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文