从SQL Server列中删除相同的数据

时间:2016-05-24 07:54:45

标签: sql-server

我想询问一下我的问题:

SELECT 
    a.Parent_Barcode,a.Parent_ItemBOM, a.Child_ItemChildNo,
    b.WorkCenter, b.OperationNo, b.OperationDesc 
FROM 
    @T_BOM a
LEFT JOIN
    ROUTING b ON a.Parent_ItemBOM = b.itemno
WHERE
    a.parent_itembom IN (SELECT itemno FROM routing)
ORDER BY 
    b.operationno

结果:

|BARCODE1 |RK2/7350/7357-4.40-850/XX69/7303FFB  |R7350/7357-4.40-850                 |  8050|50.000000| COGPAD RCVS                   
|BARCODE1 |RK2/7350/7357-4.40-850/XX69/7303FFB  |FXX69/7303FFB-1245                  |  8050|50.000000| COGPAD RCVS                   
|BARCODE1 |P-ARRGY5704-9.3-850-RE5              |F3W/7303FFB-1250                    |  8060|60.000000| BUILDING RCVS                 
|BARCODE1 |P-ARRGY5704-9.3-850-RE5              |R7350/7357-1.40-850                 |  8060|60.000000| BUILDING RCVS                 
|BARCODE1 |P-ARRGY5704-9.3-850-RE5              |E1020                               |  8060|60.000000| BUILDING RCVS                 
|BARCODE1 |P-ARRGY5704-9.3-850-RE5              |RK2/7350/7357-4.40-850/XX69/7303FFB |  8060|60.000000| BUILDING RCVS                 
|BARCODE1 |SL-ARRGY5704-18.2X9.3X31X842+N       |P-ARRGY5704-9.3-850-RE5             |  8070|70.000000| CURING RCVS                   
|BARCODE1 |ARRGY5704-18.2X9.3X31X842+N#         |SL-ARRGY5704-18.2X9.3X31X842+N      |  8080|80.000000| CUTTING RCVS
|BARCODE1 |ARRGY5704-18.2X9.3X31X842+N#         |SL-ARRGY5704-18.2X9.3X31X842+N      |  8081|81.000000| SANDING                       
|BARCODE1 |ARRGY5704-18.2X9.3X31X842+N#         |SL-ARRGY5704-18.2X9.3X31X842+N      |  8082|82.000000| POC RCVS                      
|BARCODE1 |ARRGY5704-18.2X9.3X31X842+N#         |SL-ARRGY5704-18.2X9.3X31X842+N      |  8090|90.000000| INSPECTION RCVS

=======================================================================================

|BARCODE2 |RK2/7350/7357-4.40-850/XX69/7303FFB  |R7350/7357-4.40-850                 |  8050|50.000000| COGPAD RCVS                   
|BARCODE2 |RK2/7350/7357-4.40-850/XX69/7303FFB  |FXX69/7303FFB-1245                  |  8050|50.000000| COGPAD RCVS                   
|BARCODE2 |P-ARRGY5704-9.3-850-RE5              |F3W/7303FFB-1250                    |  8060|60.000000| BUILDING RCVS                 
|BARCODE2 |P-ARRGY5704-9.3-850-RE5              |R7350/7357-1.40-850                 |  8060|60.000000| BUILDING RCVS                 
|BARCODE2 |P-ARRGY5704-9.3-850-RE5              |E1020                               |  8060|60.000000| BUILDING RCVS                 
|BARCODE2 |P-ARRGY5704-9.3-850-RE5              |RK2/7350/7357-4.40-850/XX69/7303FFB |  8060|60.000000| BUILDING RCVS                 
|BARCODE2 |SL-ARRGY5704-18.2X9.3X31X842+N       |P-ARRGY5704-9.3-850-RE5             |  8070|70.000000| CURING RCVS                   
|BARCODE2 |ARRGY5704-18.2X9.3X31X842+N#         |SL-ARRGY5704-18.2X9.3X31X842+N      |  8080|80.000000| CUTTING RCVS
|BARCODE2 |ARRGY5704-18.2X9.3X31X842+N#         |SL-ARRGY5704-18.2X9.3X31X842+N      |  8081|81.000000| SANDING                       
|BARCODE2 |ARRGY5704-18.2X9.3X31X842+N#         |SL-ARRGY5704-18.2X9.3X31X842+N      |  8082|82.000000| POC RCVS                      
|BARCODE2 |ARRGY5704-18.2X9.3X31X842+N#         |SL-ARRGY5704-18.2X9.3X31X842+N      |  8090|90.000000| INSPECTION RCVS

在那个结果中,我有两个“COGPAD RCVS”和四个“BUILDING RCVS”值。我只想消除它,所以我只为每个条形码设置一个“COGPAD RCVS”和“BUILDING RCVS”

我期待的结果:

|BARCODE1 |RK2/7350/7357-4.40-850/XX69/7303FFB  |8050|50.000000| COGPAD RCVS                                 
|BARCODE1 |P-ARRGY5704-9.3-850-RE5              |8060|60.000000| BUILDING RCVS                              
|BARCODE1 |SL-ARRGY5704-18.2X9.3X31X842+N       |8070|70.000000| CURING RCVS                   
|BARCODE1 |ARRGY5704-18.2X9.3X31X842+N#         |8080|80.000000| CUTTING RCVS
|BARCODE1 |ARRGY5704-18.2X9.3X31X842+N#         |8081|81.000000| SANDING                       
|BARCODE1 |ARRGY5704-18.2X9.3X31X842+N#         |8082|82.000000| POC RCVS                      
|BARCODE1 |ARRGY5704-18.2X9.3X31X842+N#         |8090|90.000000| INSPECTION RCVS

=======================================================================================

|BARCODE2 |RK2/7350/7357-4.40-850/XX69/7303FFB  |8050|50.000000| COGPAD RCVS                          
|BARCODE2 |P-ARRGY5704-9.3-850-RE5              |8060|60.000000| BUILDING RCVS                              
|BARCODE2 |SL-ARRGY5704-18.2X9.3X31X842+N       |8070|70.000000| CURING RCVS                   
|BARCODE2 |ARRGY5704-18.2X9.3X31X842+N#         |8080|80.000000| CUTTING RCVS
|BARCODE2 |ARRGY5704-18.2X9.3X31X842+N#         |8081|81.000000| SANDING                       
|BARCODE2 |ARRGY5704-18.2X9.3X31X842+N#         |8082|82.000000| POC RCVS                      
|BARCODE2 |ARRGY5704-18.2X9.3X31X842+N#         |8090|90.000000| INSPECTION RCVS

如何消除这样的相同数据?

2 个答案:

答案 0 :(得分:0)

实际上,您的结果集有5列。当您查看数据时,您的每一行对于5列的组合都是唯一的。

您只期望最后一列的唯一性。您可以尝试以下两个查询。

SELECT max(a.Parent_ItemBOM)
    ,max(a.Child_ItemChildNo)
    ,b.WorkCenter
    ,b.OperationNo
    ,b.OperationDesc
FROM @T_BOM a
LEFT JOIN ROUTING b ON a.Parent_ItemBOM = b.itemno
GROUP BY b.WorkCenter
    ,b.OperationNo
    ,b.OperationDesc
WHERE a.parent_itembom IN (
        SELECT itemno
        FROM routing
        )
ORDER BY b.operationno

SELECT DISTINCT b.WorkCenter
    ,b.OperationNo
    ,b.OperationDesc
FROM @T_BOM a
LEFT JOIN ROUTING b ON a.Parent_ItemBOM = b.itemno
WHERE a.parent_itembom IN (
        SELECT itemno
        FROM routing
        )
ORDER BY b.operationno

答案 1 :(得分:0)

;WITH ExpectedResult AS
(
    SELECT R.*, 
        ROW_NUMBER() OVER(PARTITION BY R.WorkCenter, R.OperationNo, R.OperationDesc ORDER BY R.WorkCenter) AS RowNumber 
    FROM (
        SELECT 
            a.Parent_ItemBOM, a.Child_ItemChildNo,
            b.WorkCenter, b.OperationNo, b.OperationDesc 
        FROM 
            @T_BOM a
        LEFT JOIN
            ROUTING b ON a.Parent_ItemBOM = b.itemno
        WHERE
            a.parent_itembom IN (SELECT itemno FROM routing)
        ORDER BY 
            b.operationno
    ) R
)

SELECT Parent_ItemBOM, Child_ItemChildNo, WorkCenter, OperationNo, OperationDesc 
FROM ExpectedResult
WHERE RowNumber = 1

CTE将为您提供帮助。通过WorkCenter,OperationNo和OperationDesc设置RowNumber组,获取RowNumber = 1将提供预期结果