繁体   English   中英

在sql中使用group by连接两个表

[英]joining two tables with group by in sql

你好,我想加入这两个表,但我不能。 我有单独的数据,但不幸的是我无法加入它们,因为表 nr 2 中有一个 group by 语句,这使得加入有点复杂。

表nr 1; -- 包括所有数据

SELECT i.attr,iv.dateofvalue,iv.price 
FROM information i, informationvalues iv
WHERE i.attr=iv.attr;

表nr2; -- 仅包括最近的数据,不包括价格

SELECT i.attr, MAX (iv.dateofvalue) AS recentdate
FROM information i, informationvalues iv
 WHERE i.attr=iv.attr 
 GROUP BY i.attr;

目标是通过从 table nr 1 获取它的相应价格来扩展 tablenr 2。注意:recentdate=dateofvalue 希望有人可以提供帮助,提前致谢!

您似乎希望提取每个属性的最新价格。 如果是这样,您可以使用相关子查询进行过滤,该查询为您提供每个属性的最后日期:

select 
    i.attr,
    v.dateofvalue,
    v.price
from information i
inner join informationvalues v on v.attr = i.attr
where v.dateofvalue = (
    select max(v1.dateofvalue) from informationvalues v1 where v1.attr = v.attr
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM