我需要一些帮助。我在sql 2012中创建了一个视图,该视图将使用人员ID#并显示输入日期和在该日期输入的余额。我试图转换余额甚至尝试增加10日的入场日期和余额金额字段。
我文件中的balance_amount在小数点前不超过5位,小数点后6位。
我需要输出:
people_org_code_id | entry_date1 | balance1 | entry_date2 | balance2
--------------------------------------------------------------------
001234567 01/01/2018 2455.33 02/26/2018 358.33
005212222 01/24/2018 25.00
等等,直到它到达entry_date15和balance15。
我到目前为止的代码是:
SELECT people_org_code_id,
[entry_date1] AS entry_date1,
[balance_amount1] AS balance1,
[entry_date2] AS entry_date2,
[balance_amount2] AS balance2,
[entry_date3] AS entry_date3,
[balance_amount3] AS balance3,
[entry_date4] AS entry_date4,
[balance_amount4] AS balance4,
[entry_date5] AS entry_date5,
[balance_amount5] AS balance5,
[entry_date6] AS entry_date6,
[balance_amount6] AS balance6,
[entry_date7] AS entry_date7,
[balance_amount7] AS balance7,
[entry_date8] AS entry_date8,
[balance_amount8] AS balance8,
[entry_date9] AS entry_date9,
[balance_amount9] AS balance9,
[entry_date10] AS entry_date10,
[balance_amount10] AS balance_amount10,
[entry_date11] AS entry_date11,
[balance_amount11] AS balance_amount11,
[entry_date12] AS entry_date12,
[balance_amount12] AS balance_amount12,
[entry_date13] AS entry_date13,
[balance_amount13] AS balance_amount13,
[entry_date14] AS entry_date14,
[balance_amount14] AS balance_amount14,
[entry_date15] AS entry_date15,
[balance_amount15] AS balance_amount15
FROM (SELECT people_org_code_id,
col + '_' + CONVERT(NVARCHAR(2), rw, 1) AS col,
val
FROM (SELECT cc.people_org_code_id,
CONVERT(NVARCHAR(13), Cast(cc.entry_date AS DATE), 1)
AS
entry_date,
CONVERT(NVARCHAR(13), Cast(
cc.balance_amount AS decimal(5,2))) AS balance_amt,
Row_number()
OVER(
partition BY cc.people_org_code_id
ORDER BY cc.entry_date)
AS rw
FROM chargecredit cc) AS q1
UNPIVOT (val
FOR col IN (entry_date, balance_amt)) AS u1) x
PIVOT (Max(val)
FOR col IN ([entry_date1],
[balance_amount1],
[entry_date2],
[balance_amount2],
[entry_date3],
[balance_amount3],
[entry_date4],
[balance_amount4],
[entry_date5],
[balance_amount5],
[entry_date6],
[balance_amount6],
[entry_date7],
[balance_amount7],
[entry_date8],
[balance_amount8],
[entry_date9],
[balance_amount9],
[entry_date10],
[balance_amount10],
[entry_date11],
[balance_amount11],
[entry_date12],
[balance_amount12],
[entry_date13],
[balance_amount13],
[entry_date14],
[balance_amount14],
[entry_date15],
[balance_amount15] )) p1
答案 0 :(得分:0)
您需要将演员表更改为演员表(cc.balance_amount AS decimal(11,6))
十进制数据类型定义如下:
DECIMAL(精确度,比例)
精确度是数字中的位数。比例是数量 数字小数点右边的数字。例如, 数字123.45的精度为5,比例为2。
有关详细信息,请参阅https://docs.microsoft.com/en-us/sql/t-sql/data-types/precision-scale-and-length-transact-sql