【问题标题】:How to formulate T-SQL to exclude duplicates?如何制定 T-SQL 以排除重复项?
【发布时间】:2010-08-24 04:31:37
【问题描述】:

我正在尝试开发一个查询以仅返回非重复记录,以便我可以将这些记录添加到我的数据库中,但我不断收到重复记录错误。

我尝试了您的解决方案,但仍然遇到重复错误问题。我删除了重复的 35 行。还有什么可能导致这种情况?这是我的查询。我认为部分混淆是 measureid 是 j5c_MasterMeasures 中的一个列,但这个值来自 j5c_ListBoxMeasures_Sys 中的两个字段。

CREATE TABLE #GOOD_RECORDS3 (STUDENTID VARCHAR(50), MEASUREDATE SMALLDATETIME, MEASUREID VARCHAR(100),
score_10 VARCHAR(100))
INSERT INTO #GOOD_RECORDS3
select A.studentid, A.measuredate, B.measurename+' ' +B.LabelName, A.score_10
from [J5C_Measures_Sys] A join [J5C_ListBoxMeasures_Sys] B on A.MeasureID = B.MeasureID
except
select A.studentid, A.measuredate, B.measurename+' ' +B.LabelName, A.score_10
from [J5C_Measures_Sys] A join [J5C_ListBoxMeasures_Sys] B on A.MeasureID = B.MeasureID
GROUP BY A.studentid, A.measuredate, B.measurename, B.LabelName, A.score_10
having COUNT(A.score_10) > 1

delete #GOOD_RECORDS3
from #GOOD_RECORDS3 a
join sysobjects so on so.name = 'J5C_Measures_Sys' AND so.type = 'u'
join syscolumns sc on so.id = sc.id and sc.name = 'score_10'
join [J5C_MeasureNamesV2_Sys] v on v.Score_field_id = sc.name
WHERE A.SCORE_10 IS NOT NULL AND A.STUDENTID IS NOT NULL AND A.MEASUREID IS NOT NULL
and exists (select 1 from J5C_MasterMeasures M
 where M.StudentID = A.StudentID
and M.MeasureID = A.MeasureID)

Insert into J5C_MasterMeasures (studentid, measuredate, measureid, nce)
select A.studentid, A.measuredate, a.MEASUREID, A.score_10
from #GOOD_RECORDS3 a
join sysobjects so on so.name = 'J5C_Measures_Sys' AND so.type = 'u'
join syscolumns sc on so.id = sc.id and sc.name = 'score_10'
join [J5C_MeasureNamesV2_Sys] v on v.Score_field_id = sc.name
WHERE A.SCORE_10 IS NOT NULL AND A.STUDENTID IS NOT NULL AND A.MEASUREID IS NOT NULL

【问题讨论】:

    标签: tsql exists


    【解决方案1】:

    您没有提到 J5C_MasterMeasures 上唯一约束的细节。因此,我假设插入的所有四列都是约束的一部分。此外,您使用Except 让我相信您使用的是SQL Server 2005 或更高版本。此外,还不清楚与J5C_MeasureNamesV2_Sys 的联接如何适合设计或解决方案。

    With GoodRecords As
        (
        Select A.StudentId
            , A.measuredate
            , B.measurename+ ' ' +B.LabelName
            , A.score_10 As NCE
        From [J5C_Measures_Sys] A 
            Join [J5C_ListBoxMeasures_Sys] B 
                On A.MeasureID = B.MeasureID
        Where A.StudentId Is Not Null
            And A.Score_10 Is Not Null
            And A.MeasureId Is Not Null
        Group By A.StudentId
            , A.MeasureDate
            , B.MeasureName+ ' ' +B.LabelName
            , A.score_10 
        Having Count(A.Score_10) = 0
        )
    Insert J5C_MasterMeasures ( StudentId, MeasureData, MeasureId, NCE )
    Select GR.StudentId, GR.MeasureData, GR.MeasureId, GR.NCE
    From GoodRecords As GR
        Join [J5C_MeasureNamesV2_Sys] v 
            On v.Score_field_id = 'Score_10'
    Where Not Exists    (
                        Select 1
                        From J5C_MasterMeasures As J1
                        Where J1.StudentId = GR.StudentId
                            And J1.MeasureData = GR.MeasureData
                            And J1.MeasureId = GR.MeasureId
                            And J1.NCE = GR.NCE
                        )
    

    【讨论】:

    • 非常感谢,托马斯。但是,当我运行此查询时,它返回 0 条记录。也许这是因为只有前 3 列包含主键。是的,我使用的是 SS 2008。J5C_MeasureNamesV2_Sys 是我的查找表——但与这个问题并不特别相关。
    猜你喜欢
    • 2011-04-19
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2015-02-13
    相关资源
    最近更新 更多