【问题标题】:需要从 select 语句的派生列更新列
【发布时间】: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


【解决方案1】:

您似乎正在尝试更新同一个表中的两个现有列?

在从现有数据计算数据的情况下使用它不是一个好的模式 - 当数据更改时,您的计算值会立即失效。

话虽如此,要执行您的要求,您只需使用可更新的 CTE,例如:

with tax as (
    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 as withouttax,
         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 as withtax
    from dbo.EmpPFContributionTest
    where
    pcm_year * 100 + pcm_month >= 201504
    and pcm_year * 100 + pcm_month < 201604
    and emp_no = 11101201
)
update tax set
EmpContbtnWithoutTax = withouttax
empcontbtnwithtax = withtax

【讨论】:

  • 这里以这种方式 EmpContbtnWithoutTax 和 empcontbtnwithtax 显示无效列。其次,正如您在未来数据更改时提到的那样,这些列值可能会变得无效。因此,为了克服这个问题,我计划在插入、更新操作时触发。
【解决方案2】:

我已经完成了视图

                    CREATE OR ALTER function fn_GetEmpPFContribution
                (
                    @year nvarchar(4)
                )
                returns table
                as
                return(
                        select  case when sum(isnull(pfc.emp_contribution,0)) over(partition by pfc.emp_no order by (pfc.pcm_year * 100 + pfc.pcm_month)) + sum(isnull(vpf,0)) over(partition by pfc.emp_no order by (pfc.pcm_year * 100 + pfc.pcm_month)) < 3000 
                                    then sum(isnull(pfc.emp_contribution,0)) over(partition by pfc.emp_no order by (pfc.pcm_year * 100 + pfc.pcm_month)) + sum(isnull(vpf,0)) over(partition by pfc.emp_no order by (pfc.pcm_year * 100 + pfc.pcm_month))
                                    else null
                                end
                                [EmpContbtnWithoutTax],
                                case when sum(isnull(pfc.emp_contribution,0)) over(partition by pfc.emp_no order by (pcm_year * 100 + pfc.pcm_month)) + sum(isnull(pfc.vpf,0)) over(partition by pfc.emp_no order by (pfc.pcm_year * 100 + pfc.pcm_month)) >= 3000
                                     then sum(isnull(pfc.emp_contribution,0)) over(partition by pfc.emp_no order by (pcm_year * 100 + pfc.pcm_month)) + sum(isnull(pfc.vpf,0)) over(partition by pfc.emp_no order by (pfc.pcm_year * 100 + pfc.pcm_month))
                                else null
                                end     
                                [EmpContbtnWithTax],
                                pfc.* 
                        from [dbo].[pf_contribution_master] pfc
                        where
                         (pfc.pcm_year * 100 + pfc.pcm_month >= concat(@year,'04')) 
                        AND
                         (pfc.pcm_year * 100 + pfc.pcm_month < concat(@year+1,'04'))
                      )

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2013-09-17
  • 2017-01-15
  • 2018-07-29
  • 1970-01-01
  • 2023-01-19
  • 2020-12-01
  • 1970-01-01
  • 2015-01-13
  • 2010-11-06
相关资源
最近更新 更多