SQL查询 - 如何抑制结果集中的重复值?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL查询 - 如何抑制结果集中的重复值?相关的知识,希望对你有一定的参考价值。

我试图压制TotalCarton列中的重复值。试图将值替换为空白或null但是失败了。有帮助吗?

这是SQL脚本:

SELECT ORDERS.StorerKey,
    ORDERS.OrderKey, 
    PackKey = (SELECT MAX(PackKey) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE ORderKey = ORDERS.OrderKey),
    PackHU = BAX_PACK_DTL.OuterPackID,
    SalesOrderNum = ( SELECT Upper(Max(ORDERDETAIL.CustShipInst01)) FROM ORDERDETAIL WITH (NOLOCK) WHERE OrderKey = ORDERS.OrderKey),
    DeliveryNum = Upper(ORDERS.ExternOrderKey),     
    TotalCarton = ( CASE BAX_PACK_DTL.PackType WHEN 'C' THEN Count(DISTINCT(BAX_PACK_DTL.OuterPackID))
                    ELSE 0 END ),
    TotalPallet = ( CASE BAX_PACK_DTL.PackType WHEN 'P' THEN Count(DISTINCT(BAX_PACK_DTL.OuterPackID))
                    ELSE 0 END ),
    SumCarton = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE PackType = 'C' AND PackKey = '0000000211'),
    SumPallet = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE PackType = 'P' AND PackKey = '0000000211'),             
    AddWho = Upper(ORDERS.EditWho), 
    ORDERS.AddDate
FROM ORDERS WITH (NOLOCK) INNER JOIN ORDERDETAIL WITH (NOLOCK) ON ORDERS.StorerKey = ORDERDETAIL.StorerKey 
                AND ORDERS.OrderKey = ORDERDETAIL.OrderKey
            INNER JOIN PICKDETAIL WITH (NOLOCK) ON ORDERDETAIL.StorerKey = PICKDETAIL.StorerKey 
                AND ORDERDETAIL.OrderKey = PICKDETAIL.OrderKey
                AND ORDERDETAIL.OrderLineNumber = PICKDETAIL.OrderLineNumber
            INNER JOIN BAX_PACK_DTL WITH (NOLOCK) ON PICKDETAIL.OrderKey = BAX_PACK_DTL.OrderKey 
                AND PICKDETAIL.PickDetailKey = BAX_PACK_DTL.PickDetailKey
WHERE (SELECT COUNT(DISTINCT(ORDERKEY)) FROM PICKDETAIL WITH (NOLOCK) WHERE OrderKey = ORDERS.OrderKey ) > 0
AND BAX_PACK_DTL.PackKey = '0000000211'
AND BAX_PACK_DTL.OuterPackID IN 
('P111111111',
'P22222222',
'P33333333')
GROUP BY ORDERS.StorerKey,
    ORDERS.OrderKey, 
    ORDERS.ExternOrderKey,
    ORDERS.HAWB,
    ORDERS.SO,
    ORDERS.EditWho, 
    ORDERS.AddDate,
    PICKDETAIL.WaveKey,
    BAX_PACK_DTL.OuterPackID,
    BAX_PACK_DTL.PackKey,
    BAX_PACK_DTL.PackType
ORDER BY BAX_PACK_DTL.OuterPackID ASC

以下是基于上述查询的当前结果集。 enter image description here

答案

你的代码看起来很奇怪。我希望查询使用条件聚合,看起来更像这样:

SELECT ORDERS.StorerKey, ORDERS.OrderKey, 
       PackKey = (SELECT MAX(PackKey) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE ORderKey = ORDERS.OrderKey),
       PackHU = BAX_PACK_DTL.OuterPackID,
       SalesOrderNum = ( SELECT Upper(Max(ORDERDETAIL.CustShipInst01)) FROM ORDERDETAIL WITH (NOLOCK) WHERE OrderKey = ORDERS.OrderKey),
       DeliveryNum = Upper(ORDERS.ExternOrderKey),     
       TotalCarton = COUNT(DISTINCT CASE BAX_PACK_DTL.PackType WHEN 'C' THEN BAX_PACK_DTL.OuterPackID END),
       TotalPallet = COUNT(DISTINCT CASE BAX_PACK_DTL.PackType WHEN 'P' THEN BAX_PACK_DTL.OuterPackID END),
       SumCarton = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL bpd WHERE pbd.PackType = 'C' AND pbd.PackKey = '0000000211'),
       SumPallet = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL bpd WHERE pbd.PackType = 'P' AND pbd.PackKey = '0000000211'),             
       AddWho = Upper(ORDERS.EditWho), 
       ORDERS.AddDate
FROM . . .
GROUP BY ORDERS.StorerKey, ORDERS.OrderKey, Upper(ORDERS.ExternOrderKey),
         Upper(ORDERS.EditWho), ORDERS.AddDate;

这可能不准确。鉴于表结构,您没有限定列名,并且使用非常神秘的查询语法,混合子查询和聚合。但它应该给出一个想法。

以上是关于SQL查询 - 如何抑制结果集中的重复值?的主要内容,如果未能解决你的问题,请参考以下文章

如何去掉SQL结果集中的 零

MS Access SQL 如何消除重复值

在 SQL 中检测到重叠,未在选择中使用子查询且未将重复记录添加到结果集中

SQL NOT EXIST 在查询结果中返回重复值

SQL NOT EXIST在查询结果中返回重复值

如何从java中的结果集中获取列名[重复]