Sql 按一列分组

Sql 按一列分组,sql,sql-server-2008,Sql,Sql Server 2008,下面是我的sql查询 select p.ProductID,p.PartNo,p.Description from tblProducts p left join (select productid,matchingmodels from tblAlternates group by productid,matchingmodels ) alt on p.productid = alt.productid where alt.matching

下面是我的sql查询

select p.ProductID,p.PartNo,p.Description
from tblProducts p left join
     (select productid,matchingmodels
      from tblAlternates
      group by productid,matchingmodels
     ) alt
     on p.productid = alt.productid
where alt.matchingmodels like '%TYT%'
这就是我的TBLProd如何切割llooks

我的tblAlternates看起来像这样

我目前的结果如下

我希望看到的结果是一行,而不是3行

期望的结果就是这样

任何帮助都将不胜感激。

“因为在子查询中,您是通过“productId”和 “matchingmodels”列您将获得“productId”的重复值,就像您在问题中所显示的那样。当您进行联接时,它将返回所有匹配项,从而返回您的重复项。由于您似乎不需要“matchingmodels”列,所以您不能在子查询中选择它,这样将处理重复项

select p.ProductID,p.PartNo,p.Description
from tblProducts p left join
     (select productid
      from tblAlternates
      where matchingmodels like '%TYT%'
      group by productid
     ) alt
     on p.productid = alt.productid


顺便说一句,你需要移动“where”“此子查询中的条件

一行上会有什么?请展示你想要的结果。你在使用什么数据库?请正确标记。如果确实要左连接,请将
alt.matchingmodels(如“%TYT%”)移动到ON子句。
选择不同的p.ProductID、p.PartNo、p.Description
。。。?
select p.ProductID,p.PartNo,p.Description
from tblProducts p left join
     (select DISTINCT productid
      from tblAlternates  
       where matchingmodels like '%TYT%'   
     ) alt
     on p.productid = alt.productid