在 sql 中使用 group by 连接两个表
joining two tables with group by in sql
你好,我正在尝试加入这两个 table,但我无法加入。
我有单独的数据,但不幸的是我不能加入它们,因为 table nr 2 中涉及一个 group by 语句,这使得连接有点复杂。
table nr 1; -- 包括所有数据
SELECT i.attr,iv.dateofvalue,iv.price
FROM information i, informationvalues iv
WHERE i.attr=iv.attr;
tablenr2; -- 仅包括不包括价格的近期数据
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 及其各自的价格。
注意:最近的日期=日期值
希望有人能帮忙,提前致谢!
您似乎想找出每个属性的最新价格。如果是这样,您可以使用相关子查询进行过滤,该子查询为您提供每个属性的最后日期:
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
)
你好,我正在尝试加入这两个 table,但我无法加入。 我有单独的数据,但不幸的是我不能加入它们,因为 table nr 2 中涉及一个 group by 语句,这使得连接有点复杂。
table nr 1; -- 包括所有数据
SELECT i.attr,iv.dateofvalue,iv.price
FROM information i, informationvalues iv
WHERE i.attr=iv.attr;
tablenr2; -- 仅包括不包括价格的近期数据
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 及其各自的价格。 注意:最近的日期=日期值 希望有人能帮忙,提前致谢!
您似乎想找出每个属性的最新价格。如果是这样,您可以使用相关子查询进行过滤,该子查询为您提供每个属性的最后日期:
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
)