如何去掉SQL结果集中的 零

Posted

tags:

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

一个表中 很多字段都是 Int型的,当没有数据录入的时候,Int型的字段默认保存为0,
当我select * from table 时, 结果集中 就会出现很多0,跪求 怎么去掉,我在查询时 把int型的字段转换成varchar 也不行的,,,,,,,

用这个就可以了。(把id换为你的列名)
case id when 0 then '' else convert(varchar(100),id,1) end
参考技术A select
case 字段1 when 0 then '' else cast(字段1 as varchar) end,
case 字段2 when 0 then '' else cast(字段2 as varchar) end,
case 字段3 when 0 then '' else cast(字段3 as varchar) end,
... ...
from 表
参考技术B 用case when 判断下。
case when isnull(core,0) = 0 then '' else isnull(core,0) end
参考技术C select case when 字段名= 0 then '' else cast(字段名 as nvarchar(50)) end as a,... from table_name

这个应该是你要的结果。

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 将单元ID和“硬编码”数据选择到查询结果集中,该结果集可以粘贴到sql开发人员中,用于缺少特定单元的单元

从 Oracle 中的动态 SQL 获取结果集中的结果

JDBC结果集

PostgreSQL查询结果集去掉返回行数

如何在sql server用户定义函数中循环查询结果集?

如何在单个结果集中列出 SQL Server 中所有数据库中的所有表?