2015-05-05 72 views
0

我正在使用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不适用于此。

回答

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] 

当然,你会用你所需要的替换你的聚合函数。

〜干杯

+0

谢谢你,而不是平均[w/o]我用min()得到最低的结果。 – Elesmoth

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