English 中文(简体)
在更新说明中处理其余事项
原标题:Handling remainders within an update statement

我要问一下的是,我要将税收额从A10501的细目上分配。 这一工作,但我想在更新的最后一段中增加任何剩余数额。

考虑A20601,共计14.14美元,需要在A10501中分四个项目。 每条线路如果平均分布,就会达到3.535,但我希望更新的是:

Line 1: 3.53
Line 2: 3.53
Line 3: 3.53
Line 4: 3.55 = total of 14.14

这里,我要问:

-- Replace  your_invcnmbr_value  with the specific INVCNMBR you want to allocate
DECLARE @target_invcnmbr NVARCHAR(50) =  INV001 ;

-- Create a local temporary table to store the total amount and row count from A20601
CREATE TABLE #temp_table (
    RowNum INT,
    INVCNMBR NVARCHAR(50),
    SumTXDTLAMT DECIMAL(18, 2),
    rowz INT
);

INSERT INTO #temp_table (RowNum, INVCNMBR, SumTXDTLAMT, rowz)
SELECT
    ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RowNum,
    INVCNMBR,
    ROUND(SUM(TXDTLAMT), 2) AS SumTXDTLAMT,
    COUNT(*) AS rowz
FROM
    A20601
WHERE
    INVCNMBR = @target_invcnmbr
GROUP BY
    INVCNMBR;

-- Update A10501 table
UPDATE A10501
SET
FEETXAMT = ROUND(FEETXAMT + (t.SumTXDTLAMT / t.rowz) + 
             CASE WHEN t.RowNum = t.rowz THEN t.SumTXDTLAMT % t.rowz ELSE 0 END, 2),
OTFTXAMT = ROUND(OTFTXAMT + (t.SumTXDTLAMT / t.rowz) +
             CASE WHEN t.RowNum = t.rowz THEN t.SumTXDTLAMT % t.rowz ELSE 0 END, 2)
FROM
P10501 p
JOIN #temp_table t ON p.INVCNMBR = t.INVCNMBR
WHERE
p.INVCNMBR = @target_invcnmbr;

-- Drop the temporary table when done
DROP TABLE #temp_table;

我想有两句子来更新表格,其余部分在更新的最后一行中增加。 现在,它将产生三个错误,我们不想这样做。

问题回答

To allocate the tax amount over the lines in A10501 while ensuring two decimal places and adding any remainder to the last line, you can modify your query as follows:

-- Replace  your_invcnmbr_value  with the specific INVCNMBR you want to allocate
DECLARE @target_invcnmbr NVARCHAR(50) =  INV001 ;

-- Create a local temporary table to store the total amount and row count from A20601
CREATE TABLE #temp_table (
    RowNum INT,
    INVCNMBR NVARCHAR(50),
    SumTXDTLAMT DECIMAL(18, 2),
    rowz INT
);

INSERT INTO #temp_table (RowNum, INVCNMBR, SumTXDTLAMT, rowz)
SELECT
    ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RowNum,
    INVCNMBR,
    SUM(TXDTLAMT) AS SumTXDTLAMT,
    COUNT(*) AS rowz
FROM
    A20601
WHERE
    INVCNMBR = @target_invcnmbr
GROUP BY
    INVCNMBR;

-- Calculate the total tax amount and the remainder
DECLARE @total_tax DECIMAL(18, 2), @remainder DECIMAL(18, 2);

SELECT @total_tax = SumTXDTLAMT FROM #temp_table WHERE RowNum = 1;
SELECT @remainder = @total_tax % rowz FROM #temp_table WHERE RowNum = rowz;

-- Update A10501 table
UPDATE A10501
SET
    FEETXAMT = ROUND(FEETXAMT + (SumTXDTLAMT / rowz) + 
                 CASE WHEN RowNum = rowz THEN @remainder ELSE 0 END, 2),
    OTFTXAMT = ROUND(OTFTXAMT + (SumTXDTLAMT / rowz) +
                 CASE WHEN RowNum = rowz THEN @remainder ELSE 0 END, 2)
FROM
    P10501 p
JOIN #temp_table t ON p.INVCNMBR = t.INVCNMBR
WHERE
    p.INVCNMBR = @target_invcnmbr;

-- Drop the temporary table when done
DROP TABLE #temp_table;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签