根据一列中的值删除重复行

Remove duplicate rows based on values from one column

我希望第一个查询的结果覆盖第二个查询的结果:

SELECT 
    CONVERT(DATE,a.ins_timestamp) AS 'Date',  
    a.Prod_code, 
    a.Curr_boxes, 
    a.Label_barcode, 
    b.From_ord_no, 
    NULL AS To_ord_no, 
    CASE
        WHEN a.From_batch >= a.To_batch THEN a.From_batch 
        WHEN a.To_batch >= a.From_batch THEN a.To_batch 
        ELSE a.From_batch
    END AS 'Batch',  
    a.Weight, 
    'IN' AS 'Direction' 
FROM 
    a 
JOIN 
    b ON a.Label_barcode = b.Label_barcode 
WHERE 
    (a.ins_timestamp BETWEEN ? AND ?) 
    AND (a.To_batch = ?) 
    AND (a.From_batch = 0) 
    AND (a.Type = 'Consumption') 
    AND (a.To_status <> 'STOCK') 
    AND (b.From_status = 'PORDER') 
GROUP BY 
    CONVERT(DATE,a.ins_timestamp),  
    a.Prod_code, 
    a.Curr_boxes, 
    a.Label_barcode, 
    b.From_ord_no, 
    a.From_batch, 
    a.To_batch, 
    a.Weight, 
    a.From_status,
    a.To_status

UNION 

SELECT 
    CONVERT(DATE,b.ins_timestamp) AS 'Date',  
    b.Prod_code, 
    b.Curr_boxes, 
    b.Label_barcode, 
    NULL AS From_ord_no, 
    NULL AS To_ord_no, 
    CASE
        WHEN b.From_batch >= b.To_batch THEN b.From_batch 
        WHEN b.To_batch >= b.From_batch THEN b.To_batch 
        ELSE                             b.From_batch
    END AS 'Batch',  
    b.Weight, 
    'IN' AS 'Direction' 
FROM 
    b 
WHERE 
    (b.From_batch = 0) 
    AND (b.Type = 'Consumption') 
    AND (b.ins_timestamp BETWEEN ? AND ?) 
    AND (b.To_batch = ?) 
    AND (b.To_status<>'STOCK') 
GROUP BY 
    CONVERT(DATE,b.ins_timestamp),  
    b.Prod_code, 
    b.Curr_boxes, 
    b.Label_barcode,  
    b.From_batch, 
    b.To_batch, 
    b.Weight, 
    b.From_status,
    b.To_status

请注意:问号是将在 Excel 中传递的参数。

当前输出:

第二个查询的结果将为 'From_order_no' 列 return 空值,但 select 语句中的其他所有内容将完全相同。在这种情况下,合并将无法删除重复的行,因此相同的 label_barcode.

将有两行数据

期望的输出:

所以问题是 - 如何从第二个查询中删除显示 From_order_no 的结果并保留第一个查询的结果?

如果 From_ord_no 是要忽略的唯一区别,您可以对查询结果进行分组

select 'Date',  
    Prod_code, 
    Curr_boxes, 
    Label_barcode, 
    max(From_ord_no) From_ord_no, 
    To_ord_no, 
    'Batch',  
    Weight, 
    'Direction' 
from (
  <your query>
) t
group by 'Date',  
    Prod_code, 
    Curr_boxes, 
    Label_barcode, 
    To_ord_no, 
    'Batch',  
    Weight, 
    'Direction'

您可以创建临时 table。在下面的示例中,这称为#newtable。主题标签很重要,因为这实际上是使它成为 'temporary' table 的原因(不是每个人都解释这一点)。

以下内容可能对其他人有用,因为它包含大多数在线示例所没有的 WHERE 条件:

-- First create your temp table 
SELECT CONVERT(DATE,a.ins_timestamp) AS 'Date',  
a.Prod_code, 
a.Curr_boxes, 
a.Label_barcode, 
b.From_ord_no, 
NULL AS To_ord_no, 
CASE
    WHEN a.From_batch >= a.To_batch THEN a.From_batch 
    WHEN a.To_batch >= a.From_batch THEN a.To_batch 
    ELSE                             a.From_batch
END AS 'Batch',  
a.Weight, 
'IN' AS 'Direction' 

INTO #newtable 

FROM a 

JOIN b ON a.Label_barcode = b.Label_barcode 

WHERE (a.ins_timestamp Between ? And ?) AND (a.To_batch = ?) AND (a.From_batch = 0) AND (a.Type='Consumption') AND (a.To_status<>'STOCK') AND (b.From_status = 'PORDER') 

-- Now we insert the second query into the already created table
    INSERT INTO #newtable 

SELECT CONVERT(DATE,b.ins_timestamp) AS 'Date',  
b.Prod_code, 
b.Curr_boxes, 
b.Label_barcode, 
NULL AS From_ord_no, 
NULL AS To_ord_no, 
CASE
    WHEN b.From_batch >= b.To_batch THEN b.From_batch 
    WHEN b.To_batch >= b.From_batch THEN b.To_batch 
    ELSE                             b.From_batch
END AS 'Batch',  
b.Weight, 
'IN' AS 'Direction' 

FROM b 

WHERE (b.From_batch = 0) AND (b.Type='Consumption') AND (b.ins_timestamp Between ? And ?) AND (b.To_batch = ?) AND (b.To_status<>'STOCK') 

-- Now we can select whatever we want from our temp table
SELECT Date,  
    Prod_code, 
    Curr_boxes, 
    Label_barcode, 
    max(From_ord_no) From_ord_no, 
    To_ord_no, 
    Batch,  
    Weight, 
    Direction 

FROM #newtable 

GROUP BY Date,  
    Prod_code, 
    Curr_boxes, 
    Label_barcode, 
    To_ord_no, 
    Batch,  
    Weight, 
    Direction