1

我有一张带有评分结果的表格。一些结果具有相同的分数。我想按 TrackingNumber 对所有结果进行分组,然后选择得分最低的记录。如果组内有平局(组中的最低分数出现多次)我不想选择它。这是帮助解释的tsql:

CREATE TABLE Dupe
(
    Id      INT NOT NULL IDENTITY( 1, 1 ) PRIMARY KEY,
    TrackingNumber  INT NOT NULL,
    Name    VARCHAR(50),
    Score   INT NOT NULL
)
GO

INSERT  INTO dbo.Dupe ( TrackingNumber, Name, Score )VALUES ( 1, 'Name1', 1 )
INSERT  INTO dbo.Dupe ( TrackingNumber, Name, Score )VALUES ( 1, 'Name1', 3 )
INSERT  INTO dbo.Dupe ( TrackingNumber, Name, Score )VALUES ( 1, 'Name1', 3 )
INSERT  INTO dbo.Dupe ( TrackingNumber, Name, Score )VALUES ( 1, 'Name1', 5 )
INSERT  INTO dbo.Dupe ( TrackingNumber, Name, Score )VALUES ( 2, 'Name2', 1 )
INSERT  INTO dbo.Dupe ( TrackingNumber, Name, Score )VALUES ( 2, 'Name2', 4 )
INSERT  INTO dbo.Dupe ( TrackingNumber, Name, Score )VALUES ( 2, 'Name2', 9 )
INSERT  INTO dbo.Dupe ( TrackingNumber, Name, Score )VALUES ( 4, 'Name4', 11 )
INSERT  INTO dbo.Dupe ( TrackingNumber, Name, Score )VALUES ( 4, 'Name4', -55 )
INSERT  INTO dbo.Dupe ( TrackingNumber, Name, Score )VALUES ( 4, 'Name4', -55 )

期望的输出:

| ID | TRACKINGNUMBER |  NAME | SCORE |
---------------------------------------
|  1 |              1 | Name1 |     1 |
|  4 |              2 | Name2 |     1 |

有人可以指出我如何做到这一点的正确方向吗?

4

1 回答 1

1

我的查询有点复杂,但它可以完成工作:

;WITH tmp (tn, sc) AS
(
    SELECT TrackingNumber, score
    FROM dupe
    GROUP BY TrackingNumber, score
    HAVING COUNT(*) = 1
)
SELECT dupe.*
FROM dupe
INNER JOIN (
    SELECT tn , MIN(sc) AS s
    FROM tmp
    GROUP BY tn
) t2 ON tn = trackingnumber AND s = score

CTEtmp过滤掉具有单个分数的所有记录(因为不接受多个最小分数)。然后将该表再次分组以找到最小单个分数,然后dupe再次与实际的sorce表连接以输出所有列。

http://sqlfiddle.com/#!6/eadec/5

您也可以在没有 CTE 的情况下编写它(仅使用派生表):

SELECT dupe.* FROM dupe
INNER JOIN 
( SELECT tn,min(sc) s FROM 
   ( SELECT TrackingNumber tn, score sc FROM dupe 
     GROUP BY TrackingNumber, score HAVING COUNT(*)=1
   ) tmp GROUP BY tn
) t2 
ON tn=trackingnumber AND s=score

http://sqlfiddle.com/#!6/eadec/6

输出:

| ID | TRACKINGNUMBER |  NAME | SCORE |
---------------------------------------
|  1 |              1 | Name1 |     1 |
|  4 |              2 | Name2 |     1 |
|  7 |              4 | Name4 |    11 |

好的,如果您希望完全忽略具有双最小值的跟踪编号,请执行以下操作:

SELECT dupe.* FROM dupe INNER JOIN 
(SELECT tn, s FROM dupe INNER JOIN
  (SELECT trackingnumber tn,min(score) s FROM dupe 
   GROUP BY trackingnumber) t1
 ON trackingnumber=tn AND score=s
 GROUP BY tn,s HAVING COUNT(*)=1
) t2 ON trackingnumber=tn AND score=s

得到这个

| ID | TRACKINGNUMBER |  NAME | SCORE |
---------------------------------------
|  1 |              1 | Name1 |     1 |
|  4 |              2 | Name2 |     1 |

http://sqlfiddle.com/#!6/eadec/11

于 2013-08-21T13:37:58.897 回答