DISTINCT仍然在结果中给我重复记录

时间:2011-11-02 05:02:45

标签: tsql

我的每个部分都得到了双重结果......所以我显然没有在这里使用Distinct或者需要使用分组?

示例:

select DISTINCT p.PartNum,
                p.PartID,
                pn.Name,
                d.[Description],
                n.Note as PartNote
from Part p
            join PartName pn on pn.PartNameID = p.PartNameID
            join ApplicationPaint ap on ap.partID = p.PartID
            join [Application] a on a.ApplicationID = ap.ApplicationID
            join [Description] d on d.DescriptionID = ap.DescriptionID
            join Note n on n.NoteID = a.NoteID
            join MYConfig mmy on mmy.MMYConfigID = a.MYConfigID
            join Model mo on mo.ModelID = mmy.ModelID
where mmy.ModelId = 2673
and substring(n.Note, CHARINDEX(']', n.Note) + 2, LEN(n.Note))= 'Johnson'

结果:

T50015  765963  Some Part Name  SomeNoteA   [342] Johnson
T50015  765963  Some Part Name  SomeNoteA   [343] Johnson
T60024  766068  Some Part Name  SomeNoteB   [342] Johnson
T60024  766068  Some Part Name  SomeNoteB   [343] Johnson
T60231  766093  Some Part Name  SomeNoteA   [342] Johnson
T60231  766093  Some Part Name  SomeNoteA   [343] Johnson
T60232  766094  Some Part Name  SomeNoteA   [342] Johnson
T60232  766094  Some Part Name  SomeNoteA   [343] Johnson
T70134  766150  Some Part Name  SomeNoteA   [342] Johnson
T70134  766150  Some Part Name  SomeNoteA   [343] Johnson
T70230  766153  Some Part Name  SomeNoteC   [342] Johnson
T70230  766153  Some Part Name  SomeNoteC   [342] Johnson
T70230  766153  Some Part Name  SomeNoteC   [343] Johnson
Y50078  766253  Some Part Name  SomeNoteH   [342] Johnson
N30026  766352  Some Part Name  SomeNoteT   [342] Johnson
N30026  766352  Some Part Name  SomeNoteT   [343] Johnson
N50041  766465  Some Part Name  SomeNoteK   [342] Johnson
N50041  766465  Some Part Name  SomeNoteK   [343] Johnson
N60176  766499  Some Part Name  SomeNoteX   [342] Johnson
N60176  766499  Some Part Name  SomeNoteX   [343] Johnson
N60750  766503  Some Part Name  SomeNoteU   [342] Johnson
N60750  766503  Some Part Name  SomeNoteU   [343] Johnson

所以我在每个PartNumber上都会获得三倍甚至三倍

T70230  766153  Some Part Name  SomeNoteC   [342] Johnson
T70230  766153  Some Part Name  SomeNoteC   [342] Johnson
T70230  766153  Some Part Name  SomeNoteC   [343] Johnson

T50015  765963  Some Part Name  SomeNoteA   [342] Johnson
T50015  765963  Some Part Name  SomeNoteA   [343] Johnson

所以我想看到的是:

T50015  765963  Some Part Name  SomeNoteA   [342] Johnson
T60024  766068  Some Part Name  SomeNoteB   [342] Johnson
T60231  766093  Some Part Name  SomeNoteA   [342] Johnson
T60232  766094  Some Part Name  SomeNoteA   [342] Johnson
T70134  766150  Some Part Name  SomeNoteA   [342] Johnson
T70230  766153  Some Part Name  SomeNoteC   [342] Johnson
Y50078  766253  Some Part Name  SomeNoteH   [342] Johnson
N30026  766352  Some Part Name  SomeNoteT   [342] Johnson
N50041  766465  Some Part Name  SomeNoteK   [342] Johnson
N60176  766499  Some Part Name  SomeNoteX   [342] Johnson
N60750  766503  Some Part Name  SomeNoteU   [342] Johnson

所以我只希望每个唯一零件编号只有一行,而不是显示此处显示的零件编号行。

所以换句话说,例如我想要这个(仅一行为partID):

T70230  766153  Some Part Name  SomeNoteC   [342] Johnson

VS。的DUP:

T70230  766153  Some Part Name  SomeNoteC   [342] Johnson
T70230  766153  Some Part Name  SomeNoteC   [342] Johnson
T70230  766153  Some Part Name  SomeNoteC   [343] Johnson

3 个答案:

答案 0 :(得分:2)

您已从示例中省略了部分名称和注释,但我相信DISTINCT表示它应该忽略结果中的行,其中所有列的指定都是重复的,而不是任何

因为您已指定p.PartNump.PartIDpn.Named.[Description]n.Note,所以只有所有这些重复的值将被删除。

例如,您说过您的结果包括:

T70230  766153  Some Part Name  SomeNoteC   [342] Johnson
T70230  766153  Some Part Name  SomeNoteC   [342] Johnson
T70230  766153  Some Part Name  SomeNoteC   [343] Johnson

如果这些行真的是:

T70230  766153  CoolWidget1  "So much fun!"     [342] Johnson
T70230  766153  CoolWidget1  "Buy one today!"   [342] Johnson
T70230  766153  CoolWidget2  "Buy one today!"   [343] Johnson

然后所有三行都将保留,因为对于DISTINCT运算符列出的所有五个列名,没有完全相同的值。

答案 1 :(得分:0)

正在发生的事情是您的联接中有重复的行。请记住,Distinct不一定会过滤选择列表中的列(请在此处阅读:http://weblogs.sqlteam.com/jeffs/archive/2007/12/13/select-distinct-order-by-error.aspx)。

有许多解决方案,其中包括:

SELECT DISTINCT * FROM (
select          p.PartNum,
                p.PartID,
                pn.Name,
                d.[Description],
                n.Note as PartNote
from Part p
            join PartName pn on pn.PartNameID = p.PartNameID
            join ApplicationPaint ap on ap.partID = p.PartID
            join [Application] a on a.ApplicationID = ap.ApplicationID
            join [Description] d on d.DescriptionID = ap.DescriptionID
            join Note n on n.NoteID = a.NoteID
            join MYConfig mmy on mmy.MMYConfigID = a.MYConfigID
            join Model mo on mo.ModelID = mmy.ModelID
where mmy.ModelId = 2673
and substring(n.Note, CHARINDEX(']', n.Note) + 2, LEN(n.Note))= 'Johnson'
)

使用GROUP BY而不是distinct来修改创建重复行的JOIN。类似的东西:

select DISTINCT p.PartNum,
                p.PartID,
                pn.Name,
                d.[Description],
                n.Note as PartNote
from Part p
            join (SELECT Distinct Name, PartNameID 
                  FROM PartName) pn ON pn.PartNameId = p.PartNameID
            join ApplicationPaint ap on ap.partID = p.PartID
            join [Application] a on a.ApplicationID = ap.ApplicationID
            join [Description] d on d.DescriptionID = ap.DescriptionID
            join Note n on n.NoteID = a.NoteID
            join MYConfig mmy on mmy.MMYConfigID = a.MYConfigID
            join Model mo on mo.ModelID = mmy.ModelID
where mmy.ModelId = 2673
and substring(n.Note, CHARINDEX(']', n.Note) + 2, LEN(n.Note))= 'Johnson'

答案 2 :(得分:0)

DISTINCT将组合行,但它们必须完全相同。从您的选择中删除PartNote字段将为您提供唯一的设置。

要获得您在示例中显示的PartNote,以下内容应该有效......

    select DISTINCT p.PartNum,
            p.PartID,
            pn.Name,
            d.[Description],
            min(PartNote)
    ....
    group by p.PartNum, p.PartID, pn.Name, d.[Description]