查询结果中的重复

时间:2015-05-05 23:43:12

标签: sql-server

我正在使用SQL Server来组织C#中生成的数据。 但是,当我检查表中的数据时,存在问题。

以下是样本结果:

Title                           Author Names                      with  w/o
Estimating the usefulness ...   W Meng KL Liu C Yu W Wu N Rishe     71  64

Estimating the usefulness ...   W Meng KL Liu C Yu W Wu N Rishe     71  58

Estimating the usefulness ...   W Meng KL Liu C Yu W Wu N Rishe     71  54

Estimating the usefulness ...   W Meng KL Liu C Yu W Wu N Rishe     71  53

The effect of negation ...      L Jia  C Yu   W Meng                66  65

The effect of negation ...      L Jia  C Yu   W Meng                66  65

The effect of negation ...      L Jia  C Yu   W Meng                66  65

The effect of negation ...      L Jia  C Yu   W Meng                66  65

我应该写什么样的查询才能得到这个结果:

Estimating the usefulness ...   W Meng KL Liu C Yu W Wu N Rishe     71  53

The effect of negation ...      L Jia  C Yu   W Meng                66  65

感谢您的时间和帮助。

注意:Distinct不起作用。

2 个答案:

答案 0 :(得分:1)

我会使用group by语句。

SELECT [Title]
     ,[Author Names]
     ,AVG([with]) as [Avg With]
     ,AVG([w/o]) as [w/o] 
FROM [table name here]
GROUP BY [title]
     ,[Author]

当然,您将根据需要替换聚合函数。

〜干杯

答案 1 :(得分:0)

您可以使用窗口功能将结果限制在CTE,然后根据该结果进行查询。

;WITH C AS(
    SELECT ROW_NUMBER() OVER(PARTITION BY Title , [Author Names] ORDER BY Title , [Author Names], [w/o]) AS Rn
            ,Title, [Author Names], [With], [w/o]
    FROM @tbl
)
SELECT * FROM C
WHERE Rn = 1