删除重复数据复杂的SQL查询

时间:2014-07-11 15:29:18

标签: sql sql-server

我有以下复杂查询

SELECT TOP 20
    [image].Image_id, Source_Link
FROM 
    Item
INNER JOIN 
    [Image] ON Item.Image_id = [Image].Image_id
INNER JOIN 
    Age_Item ON Item.Item_id = Age_Item.Item_id
WHERE 
    Gender = 'male'
    AND Age_Item.Age_id = 4
    AND [Image].Image_id NOT IN
        (SELECT [Image].Image_id
         FROM [Image]
         INNER JOIN Item on [Image].Image_id = Item.Image_id
         INNER JOIN Session_Item on Item.Item_id = Session_Item.Item_id
         WHERE Session_Item.Session_id = 3)
ORDER BY 
    NEWID() -- random rows

然而,我意识到有时它会显示一些重复的结果,我想删除重复的行

我尝试SELECT DISTINCT,但它显示了此错误:

  

如果指定了SELECT DISTINCT,则ORDER BY项必须出现在选择列表中。

我在这种情况下无法使用GROUP BY([Image].Image_id),因为列[Image].Source_Link未包含在聚合函数中。

我该怎么做才能解决这个问题?

//编辑: 我已经修好了,感谢你们的回答

SELECT TOP 20 * FROM
(
SELECT DISTINCT [image].Image_id, Source_Link
FROM Item
INNER JOIN [Image] ON Item.Image_id = [Image].Image_id
INNER JOIN Age_Item ON Item.Item_id = Age_Item.Item_id
WHERE Gender='male'
AND Age_Item.Age_id = 4
AND [Image].Image_id NOT IN
(
    SELECT [Image].Image_id
    FROM [Image]
    INNER JOIN Item on [Image].Image_id = Item.Image_id
    INNER JOIN Session_Item on Item.Item_id = Session_Item.Item_id
    WHERE Session_Item.Session_id = 3
)
)query ORDER BY NEWID();

2 个答案:

答案 0 :(得分:1)

SELECT DISTINCT * FROM ( <your original query> ) query;

我刚刚尝试了以下内容:

SELECT DISTINCT * FROM (
SELECT TOP 20 [image].Image_id, Source_Link
FROM Item
INNER JOIN [Image] ON Item.Image_id = [Image].Image_id
INNER JOIN Age_Item ON Item.Item_id = Age_Item.Item_id
WHERE Gender='male'
AND Age_Item.Age_id = 4
AND [Image].Image_id NOT IN
(
    SELECT [Image].Image_id
    FROM [Image]
    INNER JOIN Item on [Image].Image_id = Item.Image_id
    INNER JOIN Session_Item on Item.Item_id = Session_Item.Item_id
    WHERE Session_Item.Session_id = 3
)
ORDER BY NEWID()) query;

......它编译得很好。

答案 1 :(得分:1)

由于您需要一个随机的20个随机和随机的顺序,您需要获得一个不同的集合,然后再应用TOP 20 ORDER BY NEW_ID

你可以用CTE

来做到这一点
with cte as (

SELECT DISTINCT [image].Image_id, Source_Link
FROM Item
INNER JOIN [Image] ON Item.Image_id = [Image].Image_id
INNER JOIN Age_Item ON Item.Item_id = Age_Item.Item_id
WHERE Gender='male'
AND Age_Item.Age_id = 4
AND [Image].Image_id NOT IN
(
    SELECT [Image].Image_id
    FROM [Image]
    INNER JOIN Item on [Image].Image_id = Item.Image_id
    INNER JOIN Session_Item on Item.Item_id = Session_Item.Item_id
    WHERE Session_Item.Session_id = 3
)
)
Select TOP 20 DISTINCT * FROM CTE ORDER BY NEWID()