我正在尝试返回符合此条件的结果
ProfileType
和2个不同的ColorCode
相同我当前的查询返回所有ProfileType和ColorCodes
ProfileType | ColorCode
SPT1 | 8XYZ
SPT1 | 1XYZ
SPT2 | 4XYZ
SPT2 | 4XYZ
SPT3 | 4XYZ
SPT3 | 9XYZ
SPT4 | 4XYZ
SPT4 | 4XYZ
我只想从上述结果集中返回这些行,因为它们是相同的ProfileType,但是具有两个不同的ColorCodes
ProfileType | ColorCode
SPT1 | 8XYZ
SPT1 | 1XYZ
SPT3 | 4XYZ
SPT3 | 9XYZ
查询
SELECT. P.ProfileType, E.ColorCode,
FROM profilegroups AS PG
INNER JOIN mfg.profiles AS P
ON PG.id = P.pickgroup
INNER JOIN exts AS E
ON E.Profile = P.Profile
AND E.ProductNumber IN ('XYZ123', 'MX231X')
AND PG.DoProcess = 1
AND P.ProfileType IN ('SPT1','SPT2','SPT3','SPT4','SPTX2','SPT31', 'SPT90');
表格
ProfileGroups
- id
Profiles
- Profile
- ProfileType
- PickGroup
Exts
- Profile
- ColorCode
Profiles和Exts有一个主键Profile,它与Profiles表中的ProfileType是不同的
答案 0 :(得分:1)
加入一个子查询,该子查询获取每种配置文件类型的不同颜色代码的数量,但仅返回具有一个以上颜色代码的颜色代码:
SELECT profileType
FROM Exts
GROUP BY profileType
HAVING COUNT(DISTINCT colorCode) > 1
完整查询如下:
SELECT. ProfileType, Color,
FROM profilegroups AS PG
INNER JOIN mfg.profiles AS P
ON PG.id = P.pickgroup
INNER JOIN exts AS E
ON E.ProfileType = P.ProfileType
INNER JOIN (
SELECT profile
FROM Exts
GROUP BY profile
HAVING COUNT(DISTINCT colorCode) > 1) M
ON M.profile = P.profile
WHERE E.ProductNumber IN ('XYZ123', 'MX231X')
AND PG.DoProcess = 1
AND P.ProfileType IN ('SPT1','SPT2','SPT3','SPT4','SPTX2','SPT31', 'SPT90')
答案 1 :(得分:0)
尝试一下:
select profileType, colorCode
from (select distinct profileType, colorCode from table) a
group by profileType
having count(*) > 1;