SQL Select - 与内连接分组?

时间:2015-06-18 12:44:26

标签: sql-server select group-by inner-join distinct

我正在使用以下查询:

SELECT TOP 12 
        INVENTABLE.ITEMNUMBER, 
        INVENTABLE.ITEMNAME1, 
        INVENTABLE.ITEMNAME2, 
        INVENTABLE.W_TILBUD, 
        INVENTABLE.COSTPRICE, 
        INVENTABLE.VENDITEMNUMBER, 
        INVENTABLE.A_PRODUCENT, 
        INVENTABLE.GROUP_, 
        INVENTABLE.A_GROSSISTLAGER, 
        INVENTABLE.SupplementaryUnits 

    FROM INVENTRANS

    INNER JOIN INVENTABLE ON INVENTABLE.ITEMNUMBER=INVENTRANS.ITEMNUMBER 

    WHERE   INVENTRANS.ACCOUNT='xxx'    AND 
            INVENTABLE.W_VISFORSIDE = 0 AND 
            INVENTABLE.W_VISWWW = 1     AND 
            INVENTABLE.BLOCKED = 0 

    ORDER BY INVENTRANS.ROWNUMBER DESC

此查询从一个表(INVENTRANS)中选择12个最近售出的项目,并从另一个表(INVENTABLE)中获取项目信息。它的工作原理应该是这样,除了我想防止同一个项目的多个(INVENTRANS.ITEMNUMBER)在同一个结果中。

我尝试过寻找解决方案并尝试使用DISTINCT或GROUP BY的几个示例,但我无法使其工作:/

任何帮助非常感谢! :)

解决方案:

;WITH CTE AS (
       SELECT  ROW_NUMBER() OVER (PARTITION BY INVENTRANS.ITEMNUMBER 
                                 ORDER BY INVENTRANS.ROWNUMBER DESC) AS rn,
           INVENTRANS.ITEMNUMBER,
           INVENTRANS.ROWNUMBER,
           INVENTABLE.ITEMNAME1, 
           INVENTABLE.ITEMNAME2, 
           INVENTABLE.W_TILBUD, 
           INVENTABLE.COSTPRICE, 
           INVENTABLE.VENDITEMNUMBER, 
           INVENTABLE.A_PRODUCENT, 
           INVENTABLE.GROUP_, 
           INVENTABLE.A_GROSSISTLAGER, 
           INVENTABLE.SupplementaryUnits 
    FROM INVENTRANS
    INNER JOIN INVENTABLE ON INVENTABLE.ITEMNUMBER=INVENTRANS.ITEMNUMBER 
    WHERE   INVENTRANS.ACCOUNT='xxx'    AND 
            INVENTABLE.W_VISFORSIDE = 0 AND 
            INVENTABLE.W_VISWWW = 1     AND 
            INVENTABLE.BLOCKED = 0 
)
SELECT TOP 12 *
FROM CTE 
WHERE rn = 1
ORDER BY ROWNUMBER DESC

2 个答案:

答案 0 :(得分:1)

您可以使用ROW_NUMBERPARTITION BY子句来识别重复项:

;WITH CTE AS (
   SELECT  ROW_NUMBER() OVER (PARTITION BY ITEMNUMBER 
                              ORDER BY ROWNUMBER DESC) AS rn,
           INVENTABLE.ITEMNUMBER, 
           INVENTABLE.ITEMNAME1, 
           INVENTABLE.ITEMNAME2, 
           INVENTABLE.W_TILBUD, 
           INVENTABLE.COSTPRICE, 
           INVENTABLE.VENDITEMNUMBER, 
           INVENTABLE.A_PRODUCENT, 
           INVENTABLE.GROUP_, 
           INVENTABLE.A_GROSSISTLAGER, 
           INVENTABLE.SupplementaryUnits 
    FROM INVENTRANS
    INNER JOIN INVENTABLE ON INVENTABLE.ITEMNUMBER=INVENTRANS.ITEMNUMBER 
    WHERE   INVENTRANS.ACCOUNT='xxx'    AND 
            INVENTABLE.W_VISFORSIDE = 0 AND 
            INVENTABLE.W_VISWWW = 1     AND 
            INVENTABLE.BLOCKED = 0 
)
SELECT TOP 12 *
FROM CTE 
WHERE rn = 1
ORDER BY ROWNUMBER DESC

CTErn > 1返回的任何记录都是重复记录,因此会被WHERE子句过滤掉。过滤掉的重复记录是具有最低ROWNUMBER值的记录。您可以更改ORDER BY的{​​{1}}条款,以便以您喜欢的方式更改此条件。

答案 1 :(得分:0)

您应该使用GROUP BY INVENTRANS.ITEMNUMBER(不是在您的内部联接中,而是在"主要查询")这将删除所有ITEMNUMBER的共同体。 DISINCT只删除完全相同的集合。

SELECT TOP 12 
        INVENTABLE.ITEMNUMBER, 
        INVENTABLE.ITEMNAME1, 
        INVENTABLE.ITEMNAME2, 
        INVENTABLE.W_TILBUD, 
        INVENTABLE.COSTPRICE, 
        INVENTABLE.VENDITEMNUMBER, 
        INVENTABLE.A_PRODUCENT, 
        INVENTABLE.GROUP_, 
        INVENTABLE.A_GROSSISTLAGER, 
        INVENTABLE.SupplementaryUnits 
FROM INVENTRANS

INNER JOIN INVENTABLE ON INVENTABLE.ITEMNUMBER=INVENTRANS.ITEMNUMBER 

WHERE   INVENTRANS.ACCOUNT='xxx'    AND 
        INVENTABLE.W_VISFORSIDE = 0 AND 
        INVENTABLE.W_VISWWW = 1     AND 
        INVENTABLE.BLOCKED = 0 

GROUP BY INVENTRANS.ITEMNUMBER

ORDER BY INVENTRANS.ROWNUMBER DESC