您可以按具有聚合功能的列联接吗?

时间:2019-07-19 11:17:19

标签: sql oracle join aggregate-functions

是否可以通过聚合函数进行联接?

假设我们在主查询中有

Select MIN(id) as MINID, product AS PRODUCT, SUB.produkt_name, SUB.product_type
FROM log_table
GROUP BY PRODUCT, SUB.produkt_name, SUB.product_type

我要加入一个有关产品的所有信息的子查询:

JOIN(某些子查询)SUB ON LOG_TABLE.MINID = SUB.ID

2 个答案:

答案 0 :(得分:2)

您可以在JOIN之前 进行汇总。

这是您想要的吗?

SELECT l.MINID, l.product AS PRODUCT, SUB.produkt_name, SUB.product_type
FROM (some subquery
     ) SUB JOIN
     (SELECT l.PRODUCT, MIN(l.id) as MINID
      FROM log_table l
      GROUP BY l.PRODUCT
     ) l
     ON l.MINID = SUB.ID
GROUP BY l.PRODUCT, SUB.produkt_name, SUB.product_type

答案 1 :(得分:0)

您可以执行以下操作

select * from
(
Select MIN(id) as MINID, product AS PRODUCT, SUB.produkt_name, SUB.product_type
FROM log_table
GROUP BY PRODUCT, SUB.produkt_name, SUB.product_type
)A JOIN (some subquery) SUB ON A.MINID=SUB.ID