1

我有一个如下的交易表

| ID | Trans Type | Date       | Qty | Total | Item Number | Work Order |
-------------------------------------------------------------------------
|  1 | Issue      | 11/27/2012 |   3 |  3.50 |        NULL |         10 |
|  2 | Issue      | 11/27/2012 |   3 |  3.50 |        NULL |         11 |
|  3 | Issue      | 11/25/2012 |   1 |  1.25 |        NULL |         12 |
|  4 | ID Issue   | 11/27/2012 |  -3 | -3.50 |         100 |       NULL |
|  5 | ID Issue   | 11/27/2012 |  -3 | -3.50 |         102 |       NULL |
|  6 | ID Issue   | 11/25/2012 |  -1 | -1.25 |         104 |       NULL |

这些事务是重复的,其中“问题”具有工作订单 ID,而“ID 问题”事务具有项目编号。我想更新“问题”交易的 [项目编号] 字段以包含项目编号。当我在日期、数量和总计上进行连接时,我会得到类似这样的信息

| ID | Trans Type | Date       | Qty | Total | Item Number | Work Order |
-------------------------------------------------------------------------
|  1 | Issue      | 11/27/2012 |   3 |  3.50 |         100 |         10 |
|  1 | Issue      | 11/27/2012 |   3 |  3.50 |         102 |         10 |
|  2 | Issue      | 11/27/2012 |   3 |  3.50 |         100 |         11 |
|  2 | Issue      | 11/27/2012 |   3 |  3.50 |         102 |         11 |
|  3 | Issue      | 11/25/2012 |   1 |  1.25 |         104 |         12 |

重复项成倍增加!我想要这个

| ID | Trans Type | Date       | Qty | Total | Item Number | Work Order |
-------------------------------------------------------------------------
|  1 | Issue      | 11/27/2012 |   3 |  3.50 |         100 |         10 |
|  2 | Issue      | 11/27/2012 |   3 |  3.50 |         102 |         11 |
|  3 | Issue      | 11/25/2012 |   1 |  1.25 |         104 |         12 |

或者这个(项目编号为两场比赛切换)

| ID | Trans Type | Date       | Qty | Total | Item Number | Work Order |
-------------------------------------------------------------------------
|  1 | Issue      | 11/27/2012 |   3 |  3.50 |         102 |         10 |
|  2 | Issue      | 11/27/2012 |   3 |  3.50 |         100 |         11 |
|  3 | Issue      | 11/25/2012 |   1 |  1.25 |         104 |         12 |

两者都可以。什么是简单的解决方案?

4

1 回答 1

0

用于SELECT DISTINCT过滤掉相同的结果,或者您可以对结果进行分区以获取每个分组中的第一项。

更新

这是说明分区方法的代码。

SELECT ID, [Trans Type], [Date], [Qty], [Total], [Item Number], [Work Order]
FROM
(
    SELECT 
        ID, [Trans Type], [Date], [Qty], [Total], [Item Number], [Work Order], ROW_NUMBER() OVER 
            (PARTITION BY ID, [Trans Type], [Date], [Qty], [Total]
             ORDER BY [Item Number]) AS ItemRank
    FROM YourTable
) AS SubQuery
WHERE ItemRank = 1
于 2012-11-27T21:36:57.117 回答