SQL Select - 分组依据和内部连接?

SQL Select - Group by along with inner join?

我正在使用以下查询:

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

此查询从一个 table (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

您应该使用 GROUP BY INVENTRANS.ITEMNUMBER(不是在您的内部联接中,而是在 "main query" 中)这将删除 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

您可以使用 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

CTE 返回的带有 rn > 1 的任何记录都是重复的,因此会被 WHERE 子句过滤掉。过滤掉的重复记录是具有最低 ROWNUMBER 值的记录。您可以更改 ROW_NUMBERORDER BY 子句以按您喜欢的方式更改此标准。