1

下午/晚上所有,

我正在寻找对以下查询的最后润色。我需要删除特定行中重复出现的列。当前使用以下SQL:

SELECT CBNEW.*
FROM CallbackNewID CBNEW 
INNER JOIN (SELECT IDNEW, MAX(CallbackDate) AS MaxDate 
FROM CallbackNewID 
GROUP BY IDNEW) AS groupedCBNEW 
ON (CBNEW.CallbackDate = groupedCBNEW.MaxDate) AND (CBNEW.IDNEW = groupedCBNEW.IDNEW);

我的结果集如下所示

ID     RecID  Comp  Rem Date_               IDNEW   IDOLD   CB? CallbackDate
138618  83209   1   0   2012-03-16 12:40:00 83209   83209   2   16-Mar-12
138619  83209   1   0   2012-03-16 12:40:00 83209   83209   2   16-Mar-12
110470  83799   1   0   2011-07-27 11:46:00 83799   83799   10  27-Jul-11
110471  83799   1   0   2011-07-27 11:46:00 83799   83799   10  27-Jul-11

然而,这给了我 CallBackDate 和 IDNEW 列中的重复值,因为在表中有一些不同的主键具有相同的 IDNEW 和 CallbackDate 值。

如果我将此结果转储到 Excel 中,我可以在第一个 ID 列上使用删除重复项,问题就解决了。

但我想要做的是确保我的结果只包括 ID 列的第一个实例,其中 IDNEW 和 CallbackDate 是重复的。

我确定我只需要附加一小段 SQL,但如果到目前为止我能找到答案,我就会被困住。

非常感激你的帮助。

4

4 回答 4

1

尝试添加MIN(ID)到内部查询,然后将其也添加到ON子句中:

SELECT CBNEW.*
FROM CallbackNewID CBNEW 
INNER JOIN (SELECT IDNEW, MIN(ID) AS MinId, MAX(CallbackDate) AS MaxDate 
FROM CallbackNewID 
GROUP BY IDNEW) AS groupedCBNEW 
ON (CBNEW.CallbackDate = groupedCBNEW.MaxDate) 
   AND (CBNEW.IDNEW = groupedCBNEW.IDNEW)
   AND (CBNEW.ID = groupedCBNEW.MinId) ;

sqlfiddle demo

于 2013-11-04T18:24:43.540 回答
1

这是一种相当“蛮力”的方法。它只获取原始查询的结果,并Min()在 [ID]、Max()[Comp] 和 [Rem] 以及GROUP BY其他所有内容上执行:

SELECT 
    Min(t.ID) AS MinOfID, 
    t.RecID, 
    Max(t.Comp) AS MaxOfComp, 
    Max(t.Rem) AS MaxOfRem, 
    t.Date_, 
    t.IDNEW, 
    t.IDOLD, 
    t.[CB?], 
    t.CallbackDate
FROM 
    (
        SELECT CBNEW.*
        FROM 
            CallbackNewID CBNEW 
            INNER JOIN 
            (
                SELECT IDNEW, MAX(CallbackDate) AS MaxDate 
                FROM CallbackNewID 
                GROUP BY IDNEW
            ) AS groupedCBNEW 
                ON (CBNEW.CallbackDate = groupedCBNEW.MaxDate) 
                AND (CBNEW.IDNEW = groupedCBNEW.IDNEW)
    ) t
GROUP BY 
    t.RecID, 
    t.Date_, 
    t.IDNEW, 
    t.IDOLD, 
    t.[CB?], 
    t.CallbackDate;

它可能不是非常优雅,但如果它有效的话......

于 2013-11-04T19:10:03.633 回答
0
SELECT
    A.*
FROM
    (SELECT
            *,
            ROW_NUMBER() OVER (PARTITION BY IDNEW ORDER BY CallbackDate DESC)
                          AS [row_num]
     FROM CallbackNewID 
    ) A
WHERE
    A.row_num = 1
于 2013-11-04T18:34:56.717 回答
0

在 MS SQL Server 中,我认为您正在寻找该ROW_NUMBER()功能。

这样的事情应该可以帮助您获得所需的内容:

SELECT
    X.*
FROM
    (
        SELECT
            *,
            ROW_NUMBER() OVER (PARTITION BY DBNEW.IDNEW, DBNEW.MaxDate) [row_num]
        FROM
            CallbackNewID CBNEW 
            INNER JOIN 
            (
                SELECT
                    IDNEW,
                    MAX(CallbackDate) AS MaxDate
                FROM
                    CallbackNewID 
                GROUP BY
                    IDNEW
            ) AS groupedCBNEW ON (CBNEW.CallbackDate = groupedCBNEW.MaxDate) AND (CBNEW.IDNEW = groupedCBNEW.IDNEW)
    ) X
WHERE
    X.row_num = 1
于 2013-11-04T18:24:40.200 回答