我需要一些帮助。我在 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