在不同账户之间拆分金额
Splitting amount between different accounts
我正在尝试编写一份报告,根据主账户的限制将每个 empid 的总金额分成他们的主账户和他们的副账户。下面是一些代码,用于制作一些示例表,以及我到目前为止的代码,以及底部显示的所需结果集。被注释掉的代码部分是我不确定如何编码的简单英语。
我正在尝试在 SQL Server 2012 上写这篇文章。
Create Table Test
(
Empid Int not null,
Account Varchar(25) not null,
MaxAmt Int not null
)
Create Table Test2
(
EmpID Int not null,
TtlAmt int not null
)
Insert Into Test
Values (748,'Primary',50)
Insert Into Test
Values (748,'Secondary',99999)
Insert Into Test
Values (750,'Primary',50)
Insert Into Test
Values (750,'Secondary',99999)
Insert Into Test
Values (752,'Primary',60)
Insert Into Test
Values (752,'Secondary',99999)
Insert Into Test2
Values (748,80)
Insert Into Test2
Values (750,75)
Insert Into Test2
Values (752,20)
Select * from Test
Select * from Test2
Select
B.EmpID
,A.Account
,Case
When A.Account = 'Primary' and B.TtlAmt >= A.Maxamt Then A.MaxAmt
When A.Account = 'Primary' and B.TtlAmt <= A.MaxAmt Then B.TtlAmt
When A.Account = 'Secondary' and B.TtlAmt > /* Max of Primary */ Then B.TtlAmt - /* Max of Primary */
When A.Account = 'Secondary' and B.TtlAmt <= /* Max of Primary */ Then Null
Else 'Error'
End as Amount
From Test as A
Join Test2 as B
on A.EmpID = B.EmpID
/*************** Desired Result would look like below *******************/
EmpID Account Amount
748 Primary 50
748 Secondary 30
750 Primary 50
750 Secondary 25
752 Primary 20
752 Secondary Null
这是开始,因为最终每个 empid 最多总共有 25 个帐户,而有些 empid 只有 1 个。
Select
B.EmpID
,A.Account
,Case
When A.Account = 'Primary' and B.TtlAmt >= A.Maxamt Then A.MaxAmt
When A.Account = 'Primary' and B.TtlAmt <= A.MaxAmt Then B.TtlAmt
When A.Account = 'Secondary' and B.TtlAmt > A2.MaxAmt Then B.TtlAmt - A2.MaxAmt
When A.Account = 'Secondary' and B.TtlAmt <= A2.MaxAmt Then Null
Else 'Error'
End as Amount
From Test as A
Join Test as A2 on A.EmpId = A2.EmpId and A2.Account = 'Primary'
Join Test2 as B
on A.EmpID = B.EmpID
根据下面的评论,最简单的方法是这样
Select
B.EmpID
,A.Account
,sum(Case
When A.Account = 'Primary' and B.TtlAmt >= A.Maxamt Then A.MaxAmt
When A.Account = 'Primary' and B.TtlAmt <= A.MaxAmt Then B.TtlAmt
When A.Account = 'Secondary' and B.TtlAmt > A2.MaxAmt Then B.TtlAmt - A2.MaxAmt
When A.Account = 'Secondary' and B.TtlAmt <= A2.MaxAmt Then 0
Else 0
End) as Amount
From Test as A
Join Test as A2 on A.EmpId = A2.EmpId and A2.Account = 'Primary'
Join Test2 as B
on A.EmpID = B.EmpID
group by B.EmpID, A.Account
我正在尝试编写一份报告,根据主账户的限制将每个 empid 的总金额分成他们的主账户和他们的副账户。下面是一些代码,用于制作一些示例表,以及我到目前为止的代码,以及底部显示的所需结果集。被注释掉的代码部分是我不确定如何编码的简单英语。
我正在尝试在 SQL Server 2012 上写这篇文章。
Create Table Test
(
Empid Int not null,
Account Varchar(25) not null,
MaxAmt Int not null
)
Create Table Test2
(
EmpID Int not null,
TtlAmt int not null
)
Insert Into Test
Values (748,'Primary',50)
Insert Into Test
Values (748,'Secondary',99999)
Insert Into Test
Values (750,'Primary',50)
Insert Into Test
Values (750,'Secondary',99999)
Insert Into Test
Values (752,'Primary',60)
Insert Into Test
Values (752,'Secondary',99999)
Insert Into Test2
Values (748,80)
Insert Into Test2
Values (750,75)
Insert Into Test2
Values (752,20)
Select * from Test
Select * from Test2
Select
B.EmpID
,A.Account
,Case
When A.Account = 'Primary' and B.TtlAmt >= A.Maxamt Then A.MaxAmt
When A.Account = 'Primary' and B.TtlAmt <= A.MaxAmt Then B.TtlAmt
When A.Account = 'Secondary' and B.TtlAmt > /* Max of Primary */ Then B.TtlAmt - /* Max of Primary */
When A.Account = 'Secondary' and B.TtlAmt <= /* Max of Primary */ Then Null
Else 'Error'
End as Amount
From Test as A
Join Test2 as B
on A.EmpID = B.EmpID
/*************** Desired Result would look like below *******************/
EmpID Account Amount
748 Primary 50
748 Secondary 30
750 Primary 50
750 Secondary 25
752 Primary 20
752 Secondary Null
这是开始,因为最终每个 empid 最多总共有 25 个帐户,而有些 empid 只有 1 个。
Select
B.EmpID
,A.Account
,Case
When A.Account = 'Primary' and B.TtlAmt >= A.Maxamt Then A.MaxAmt
When A.Account = 'Primary' and B.TtlAmt <= A.MaxAmt Then B.TtlAmt
When A.Account = 'Secondary' and B.TtlAmt > A2.MaxAmt Then B.TtlAmt - A2.MaxAmt
When A.Account = 'Secondary' and B.TtlAmt <= A2.MaxAmt Then Null
Else 'Error'
End as Amount
From Test as A
Join Test as A2 on A.EmpId = A2.EmpId and A2.Account = 'Primary'
Join Test2 as B
on A.EmpID = B.EmpID
根据下面的评论,最简单的方法是这样
Select
B.EmpID
,A.Account
,sum(Case
When A.Account = 'Primary' and B.TtlAmt >= A.Maxamt Then A.MaxAmt
When A.Account = 'Primary' and B.TtlAmt <= A.MaxAmt Then B.TtlAmt
When A.Account = 'Secondary' and B.TtlAmt > A2.MaxAmt Then B.TtlAmt - A2.MaxAmt
When A.Account = 'Secondary' and B.TtlAmt <= A2.MaxAmt Then 0
Else 0
End) as Amount
From Test as A
Join Test as A2 on A.EmpId = A2.EmpId and A2.Account = 'Primary'
Join Test2 as B
on A.EmpID = B.EmpID
group by B.EmpID, A.Account