减去金额> 100%

问题描述 投票:0回答:2

我有这个存储过程,它使用已验证项目的数量更新EtiquetasInventariadas列,并使用与EtiquetasPorInventariar上可用项目总数相比验证了多少项目的Precisão列

ALTER PROCEDURE [dbo].[spx_UPDATE_EtiquetasInventariadas]
@EtiquetasInventariadas int, 
@InventarioID int,
@LocalizacaoID int 
AS
BEGIN



UPDATE xLocalizacao
    SET EtiquetasInventariadas = EtiquetasInventariadas + @EtiquetasInventariadas, IsValid = 1
    WHERE (LocalizacaoID = @LocalizacaoID)

    UPDATE xLocalizacao
    SET Precisao = CAST(EtiquetasInventariadas AS DECIMAL) / CAST(EtiquetasPorInventariar AS DECIMAL)
    WHERE LocalizacaoID = @LocalizacaoID 

    IF NOT EXISTS(SELECT 1 FROM xLocalizacao WHERE InventarioID = @InventarioID AND isValid = 0)
    BEGIN 
        SELECT 1 
    END
    ELSE
    BEGIN
        SELECT -1
    END
END

然而,有人问我,如果EtiquetasInventariadas的数量高于EtiquetasPorInventariar的数量,它应该减去超过100%的数量,我似乎无法理解如何做到这一点的逻辑。

编辑

通过减去值是否高于100我的意思

如果结果为120%,则应显示80%

这是xLocalizacao的表定义

CREATE TABLE [dbo].[xLocalizacao](
    [LocalizacaoID] [int] IDENTITY(1,1) NOT NULL,
    [Localizacao] [nvarchar](20) NOT NULL,
    [EtiquetasPorInventariar] [int] NOT NULL,
    [EtiquetasInventariadas] [int] NOT NULL,
    [IsValid] [bit] NOT NULL,
    [InventarioID] [int] NOT NULL,
    [Precisao] [decimal](3, 2) NULL
)
sql-server
2个回答
3
投票

它可能不会立即显而易见,但如果您希望100%达到可达到的最大值且金额超过金额,那么一些创造性的减法和ABS可以帮助您。

如果你当前的值在一个名为@Precisao的变量中,那么如果它已经是0到100之间的数字,表示百分比:

100 - ABS(100 - @Precisao)

如果它是介于0.0和1.0之间的数字,那么:

1.0 - ABS(1.0 - @Precisao)

会给你你寻求的结果。

您当然可以用当前表达式替换该变量,并适当地加以括号。

来自fooplot.com

enter image description here


1
投票

一个简单的案例应该有效

SET Precisao = CASE WHEN EtiquetasInventariadas >= EtiquetasPorInventariar THEN 1.00 ELSE CAST(EtiquetasInventariadas AS DECIMAL) / CAST(EtiquetasPorInventariar AS DECIMAL) END
© www.soinside.com 2019 - 2024. All rights reserved.