Skip to content
Advertisement

Can you join by columns with aggregate functions?

Is it possible to join by aggregate functions?

Let’s say we have in the main query:

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

And I’m joining a subquery with all the info about products:

JOIN (some subquery) SUB ON LOG_TABLE.MINID=SUB.ID

Advertisement

Answer

You can do the following

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
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement