简体   繁体   中英

How to get records with distinct column value from result set in oracle?

could you please help me on the below query?

Using the query

SELECT LineGernal.Id, LineGernal.Description, LineGernal.SSMS, BaseAddOns.Id
from LineGernal Inner Join
     BaseAddOns
     on LineGernal.Id=BaseAddOns.ParentLineGernalID 

Output-

输出

Result Needed-

需要结果

Thanks Rajendra

You can use group by and min as follows:

SELECT LineGernal.Id, LineGernal.Description, LineGernal.SSMS, min(BaseAddOns.Id) as id
from LineGernal Inner Join
     BaseAddOns
     on LineGernal.Id=BaseAddOns.ParentLineGernalID 
GROUO BY LineGernal.Id, LineGernal.Description, LineGernal.SSMS

I would recommend pre-aggregation in a subquery:

select li.id as lineGeneralId, lg.description, lg.ssms, bao.id as BaseAddOnsId
from LineGernal lg 
inner join (
    select ParentLineGernalID, min(id) as id
    from BaseAddOns 
    group by ParentLineGernalID, 
) bao on lg.id = bao.ParentLineGernalID 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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