【问题标题】:Display 2 columns from one table having max count in column 3 and display computed sum of values from another table显示一个表中的 2 列,在第 3 列中具有最大计数,并显示另一个表中的计算值总和
【发布时间】:2015-11-02 02:56:05
【问题描述】:

我有一个客户表和采购表, 需要从客户表中显示 cname、cid 和 max(customer_visits) 以及客户在购买表中的总购买量。 我正在做这样的事情

select p.cid, c.cname, sum(p.total_price)
from customers c where exists
(select max(visits_made) from customers having visits_made=max(visits_made)
and cid=p.cid)
inner join purchases p on p.cid=c.cid
group by p.cid,c.cname

select p.cid, c.cname, sum(p.total_price)
(select max(visits_made) from customers c where c.cid=p.cid) 
from purchases p
inner join customers c on c.cid=p.cid
group by p.cid,c.cname

这些查询出了什么问题?

找到解决方案,必须在内连接后包含 where 子句:D

【问题讨论】:

  • 使用示例数据和所需结果编辑您的问题。

标签: sql oracle


【解决方案1】:

我认为这只是一个聚合查询:

select p.cid, c.cname, sum(p.total_price) as total_price,
       max(visits_made) as visits_made
from purchases p inner join
     customers c
     on c.cid = p.cid
group by p.cid, c.cname;

【讨论】:

  • 我只需要显示 max(visits_made) 的元组,不需要显示visits_made 列
  • 谢谢,找到了解决方案,必须在内连接后包含 where 子句:D
猜你喜欢
  • 2021-09-04
  • 1970-01-01
  • 2015-07-07
  • 2019-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
  • 1970-01-01
相关资源
最近更新 更多