【发布时间】:2022-01-23 06:03:12
【问题描述】:
我在下面的选择语句中有这个。在那里我有 2 个派生列 EmpContbtnWithoutTax 和 EmpContbtnWithTax 我想将该数据永久保存在同一个表 EmpPFContributionTest 中
需要帮助才能对 EmpPFContributionTest 表进行更新查询,该表会永久更新 EmpContbtnWithoutTax 和 EmpContbtnWithTax 列的相应行。更新我为下面首先提到的 1 列尝试的脚本
update t2
set EmpContbtnWithoutTax =
(
select case when sum(isnull(t1.emp_contribution,0)) over(partition by
t1.emp_no order by (t1.pcm_year * 100 + t1.pcm_month)) +
sum(isnull(vpf,0)) over(partition by t1.emp_no order by (t1.pcm_year *
100 + t1.pcm_month)) < 3000
then sum(isnull(t1.emp_contribution,0)) over(partition by
t1.emp_no order by (t1.pcm_year * 100 + t1.pcm_month)) +
sum(isnull(vpf,0)) over(partition by t1.emp_no order by (t1.pcm_year *
100 + t1.pcm_month))
else null
end
from EmpPFContributionTest t1
)
from EmpPFContributionTest t2
/*Actual Select Statement */
select case when sum(isnull(emp_contribution,0)) over(partition by emp_no order by (pcm_year * 100 + pcm_month)) + sum(isnull(vpf,0)) over(partition by emp_no order by (pcm_year * 100 + pcm_month)) < 3000
then sum(isnull(emp_contribution,0)) over(partition by emp_no order by (pcm_year * 100 + pcm_month)) + sum(isnull(vpf,0)) over(partition by emp_no order by (pcm_year * 100 + pcm_month))
else null
end
empcontbtnwithouttax,
case when sum(isnull(emp_contribution,0)) over(partition by emp_no order by (pcm_year * 100 + pcm_month)) + sum(isnull(vpf,0)) over(partition by emp_no order by (pcm_year * 100 + pcm_month)) >= 3000
then sum(isnull(emp_contribution,0)) over(partition by emp_no order by (pcm_year * 100 + pcm_month)) + sum(isnull(vpf,0)) over(partition by emp_no order by (pcm_year * 100 + pcm_month))
else null
end
empcontbtnwithtax,
* from [dbo].EmpPFContributionTest
where
(pcm_year * 100 + pcm_month >= 201504) AND
(pcm_year * 100 + pcm_month < 201604)
and emp_no= 11101201
order by (pcm_year * 100 + pcm_month)
【问题讨论】:
-
请告诉我们您尝试了什么以及您遇到的问题。
-
您不能创建使用聚合的计算列。相反,您最好使用
VIEW。 -
请提供样本数据和预期输出
-
@VenkataramanR 我已经分享了示例数据
标签: sql sql-server tsql