简体   繁体   中英

Select Distinct Value from Multiple tables, Where One has Multiples

I have searched for this problem but have not found anything quite like it. I have two tables, both with a field, FormID. In table A, FormID is unique. In table B, there can be multiple records with the same FormID -- table B is a problem tracker table, so if there are multiple data entry problems in the form, there will be multiple records in there.

The following query works:

select distinct b.FormID
from b
where b.FormID = a.FormID
and a.status='done'

as far it does generate a result list of unique FormIDs. However, I also need to get some other columns in this query and it's when I add those columns to the select or the join that I get ALL the duplicate FormIDs.

I have tried:

select distinct (b.FormID), a.FormType, a.Site, a.uid, b.ProbID, b.Date
from b, a
where b.FormID = a.FormID
and a.status='done'

as well as a couple of variations using joins, but they all end up with all the rows with duplicate FormIDs.

Suggestions?

Try

SELECT  b.FormID, 
        MAX(a.FormType) FormType, 
        MAX(a.Site) Site, 
        MAX(a.uid) uid, 
        MAX(b.ProbID) ProbID, 
        MAX(b.Date) Date
  FROM b INNER JOIN 
       a ON b.FormID = a.FormID
 WHERE a.status='done'
 GROUP BY b.FormID

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