简体   繁体   English

如何创建动态变量以加载SQL Server中的表中的字段

[英]How to create a dynamic variable to load a field in a table in SQL Server

I'm stuck with a scenario where i have to create a dynamic variable for a select query which hold the data of a particular filed. 我陷入了一种情况,即我必须为选择查询创建一个动态变量,以保存特定字段的数据。 Here is the scenario 这是场景

Table 'Customer' has below data 表“客户”具有以下数据

Column0 Column1 Column2     Column3     Column4 Column5
--------------------------------------------------------
H       123X                11/27/2017  C       10.23
D       123X    78462F103   11/28/2017  A       112.35
D       123X    55024U109   11/28/2017  A       25.30
H       456X                11/27/2017  B       5263.44
D       456X    78462F103   11/28/2017  A       23.00
D       456X    55024U109   11/28/2017  A       12123.00
D       456X    78462F103   11/28/2017  A       56.08
D       456X    55024U109   11/28/2017  C       45.07

Now i have to create variable when Column0='H' using the Sum(column5) and load it into Column5 when Column0='D' until the Column0='H' value changes(Table has multiple H values). 现在我必须使用Sum(column5)Column0='H'时创建变量,并在Column0='D'Column0='D'其加载到Column5 ,直到Column0='H'值更改为止(表具有多个H值)。

Example: I have to calculate the variable when Column0='H' lets say If Column0='H' then Variable=Sum(Column5) which is '5.36'. 示例:当Column0 ='H'让我说如果Column0 ='H'时,我必须计算变量,然后Variable = Sum(Column5)为'5.36'。 Now When Coulmn1='D' then Column5's value shlould be loaded as 5.63 when the When the new Column0='H' comes the value of variable changes to '6.33' and it should load the updated value into 'D' records. 现在,当Coulmn1 ='D'时,当新的Column0 ='H'到来时,变量5的值应更改为5.63,并且应将更新后的值加载到'D'记录中。

Any suggestions on this? 有什么建议吗?

Assuming that I am correct you want the total of all rows for each Column1 to be the new value in Column5 when Column0 = H you can do something like this. 假设我是对的,当Column0 = H时,您希望每个Column1的所有行的总数成为Column5中的新值,您可以执行以下操作。

Notice I had to create sample data in a table before I could do anything. 注意,在执行任何操作之前,我必须在表中创建示例数据。

declare @Something table 
(
    Column0 char(1)
    , Column1 char(4)
    , Column2 varchar(20)
    , Column3 date
    , Column4 char(1)
    , Column5 decimal (9,2)
)

insert @Something values
('H', '123X', '', '11/27/2017', 'C', 10.23)
, ('D', '123X', '78462F103', '11/28/2017', 'A', 112.35)
, ('D', '123X', '55024U109', '11/28/2017', 'A', 25.30)
, ('H', '456X', '', '11/27/2017', 'B', 5263.44)
, ('D', '456X', '78462F103', '11/28/2017', 'A', 23.00)
, ('D', '456X', '55024U109', '11/28/2017', 'A', 12123.00)
, ('D', '456X', '78462F103', '11/28/2017', 'A', 56.08)
, ('D', '456X', '55024U109', '11/28/2017', 'C', 45.07)
;

with SummaryData as
(
    select *
        , NewCol5 = case when Column0 = 'H' then sum(Column5) over(partition by Column1) else Column5 end
    from @Something
)

update SummaryData
set Column5 = NewCol5

select *
from @Something

I would very strongly urge to move away from this design immediately if not sooner. 我强烈敦促尽快将这种设计移开。 It is fraught with error. 它充满了错误。 And even worse is this looks like sales information. 更糟糕的是,这看起来像是销售信息。 With this design you can't track things like SalesTax, Shipping, discounts, etc....all the kinds of things you want to track about orders. 使用这种设计,您将无法跟踪诸如SalesTax,运输,折扣等之类的东西。。。

I was under the impression that the source was a flat file and am providing a SSIS solution. 我的印象是源是平面文件,正在提供SSIS解决方案。

read data -> Conditional Split ("H" and "D")

Dpath
Multicast -> Write out detail records
  -> Aggregate on Col1 --> Merge Join to H path

H path:
Merge Join on Col1 to Aggregated Data

Finally either AddH Col5 to Aggregate or replace col 5 with aggregate (it's not clear in question.)

Write out your header records.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM