【发布时间】:2015-12-14 12:42:57
【问题描述】:
这是我遇到的问题
示例:
- 总价值:1550.00 英镑
- 每月付款 12
这计算在 129.16666666666666667,但是你不会显示这样的货币价值,它会显示为 129.17 英镑,等于 1550.04 英镑,这是错误的
问题
是否可以从 12 个分期付款中的 11 个中删除浮动/小数值,并仅在第一次或最后一次付款中显示?
示例结果
PaymentNumber Value
1 £129.00
2 £129.00
3 £129.00
4 £129.00
5 £129.00
6 £129.00
7 £129.00
8 £129.00
9 £129.00
10 £129.00
11 £129.00
12 £131.00
感谢您的帮助,如果有任何关于我可以做到这一点的其他方法的建议,我将不胜感激..
我已经包含了我用来测试这个表的代码......并且还添加到了下面的数据中。现在使用更新语句是可以接受的,我会在以后尝试使代码更高效。我确实有一个程序正在运行,它以编程方式插入每月的细分。
我正在使用的表格信息
CREATE TABLE CustomerFinance (CustomerFinanceID int identity (1,1) not null,
TotalValueOwed decimal(12,4), --1550.00
LengthOfContract int) --LENGTGH IN MONTHS, i.e. 12
CREATE TABLE CustomerFinanceLine (CustomerFinanceLineID int identity (1,1) not null,
CustomerFinanceID int, --FOREIGN KEY LINK
PaymentNumber int, --1, 2, 3 AND SO ON
PaymentValue decimal(12,4)) --THE MONTHLY BREAKDOWN COSTS
--KEYS
alter table CustomerFinance add constraint CustomerFinanceID_PK PRIMARY KEY (CustomerFinanceID)
alter table CustomerFinanceLine add constraint CustomerFinanceLineID_PK PRIMARY KEY (CustomerFinanceLineID)
alter table CustomerFinanceLine add constraint CustomerFinanceID_FK FOREIGN KEY (CustomerFinanceID) REFERENCES CustomerFinance(CustomerFinanceID)
--PaymentNumber COUNTER (RUNS IN A PROCEDURE)
CREATE PROCEDURE FinanceCounter AS
;WITH MyCTE AS
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY CustomerFinanceID ORDER BY CustomerFinanceLineID) AS NewVariation
FROM CustomerFinanceLine
)
UPDATE MyCTE
SET PaymentNumber = NewVariation
WHERE PaymentNumber IS NULL
数据
--inserted PaymentValue as null for now, ideally i will
-- have a procedure to do this and insert the breakdowns programmatically
--for now an update statement will do fine unless its easier to insert it
INSERT INTO CustomerFinance VALUES (1550.00, 12)
INSERT INTO CustomerFinanceLine VALUES (1, 1, NULL)
INSERT INTO CustomerFinanceLine VALUES (1, 2, NULL)
INSERT INTO CustomerFinanceLine VALUES (1, 3, NULL)
INSERT INTO CustomerFinanceLine VALUES (1, 4, NULL)
INSERT INTO CustomerFinanceLine VALUES (1, 5, NULL)
INSERT INTO CustomerFinanceLine VALUES (1, 6, NULL)
INSERT INTO CustomerFinanceLine VALUES (1, 7, NULL)
INSERT INTO CustomerFinanceLine VALUES (1, 8, NULL)
INSERT INTO CustomerFinanceLine VALUES (1, 9, NULL)
INSERT INTO CustomerFinanceLine VALUES (1, 10, NULL)
INSERT INTO CustomerFinanceLine VALUES (1, 11, NULL)
INSERT INTO CustomerFinanceLine VALUES (1, 12, NULL)
【问题讨论】:
标签: sql sql-server