根据一列的值删除重复行

发布于 2025-01-22 13:19:42 字数 2255 浏览 0 评论 0原文

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

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'列的nulls,但是Select语句中的所有其他内容都将完全相同。在这种情况下,联盟将无法删除重复行,因此同一label_barcode将有两行数据。

所需的输出:

那么问题是 - 如何从第二个查询中删除结果,该查询显示了frof_order_no的nulls并将结果避免第一个查询?

I would like results from the first query to override results from the second query:

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

Please note: The question marks are parameters that will be passed in Excel.

Current Output:
enter image description here

The results from the second query will return nulls for the 'From_order_no' column, but everything else in the select statement will be the exact same. In this case the union will fail to remove duplicate rows and so there will be two rows of data for the same label_barcode.

Desired Output:
enter image description here

So the question is - how do I remove the result from the second query, that shows nulls for From_order_no and keep the results from the first query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄暮涼年 2025-01-29 13:19:42

提供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'

Provided From_ord_no is the only difference to be ignored you can group the result of your query

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'
忘东忘西忘不掉你 2025-01-29 13:19:42

您可以创建一个临时表。在下面的示例中,这称为#newtable。主题标签很重要,因为这实际上是使其成为“临时”表的原因(并非每个人都对此解释)。

以下可能对他人有用,因为它包括大多数示例没有在线的条件:

-- 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 

You can create a temporary table. In the below example this is called #newtable. The hashtag is important as this is actually what makes it a 'temporary' table (not everyone explains this).

The below might prove useful to others as it includes WHERE conditions which most examples do not have online:

-- 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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文