MS SQL - 顺序读写

时间:2016-09-14 17:06:58

标签: sql-server sequential

我已将我需要的数据过滤到表中,但意识到它在3个不同的行上。

我需要MS SQL中的帮助编码逻辑来完成以下操作。基本上,我需要的数据是3个不同的行。所有数据都需要来自“W”行,而只需要来自接下来2行的DESC列中的数据,这些数据也具有相同的日期和时间。

  1. 按顺序阅读表格
  2. 如果列AC包含“W”保存数据。
  3. 读取下一行,将数据保存在DESC列中。
  4. 读取下一行,在DESC中保存数据
  5. 将数据写入表B.
  6. 读到下一列AC包含“W,重复2-5
  7. 结束
  8. 表A

    DATE    TIME ACCT ATM AC AMT LOCATION
    7/11/16 1040 5555     C   0  DUNKIN #3          
    7/11/16 1105 5555 #1  W  -20                                                
    7/11/16 1105 5555     C   0  ATM                
    7/11/16 1105 5555     C   0  DUNKIN #3  
    7/12/16 2141 5555     C   0  BP#9   
    7/19/16 1025 5555 #2  W  -40                                            
    7/19/16 1025 5555     C   0  ATM                
    7/19/16 1025 5555     C   0  SBUCKS #1  
    7/21/16 2102 5555     C   0  BP#5
    

    表B

    DATE    TIME ACCT ATM AC AMT TRAN LOCATION
    7/11/16 1105 5555 #1  W  -20 ATM  DUNKIN #3
    7/19/16 1025 5555 #2  W  -40 ATM  STARBUCKS #1
    

1 个答案:

答案 0 :(得分:0)

这是不好的方法,但这就是你想要的:

declare @a table(mDate Date, MTime int, ACCCT int, Atm nVarChar(2), AC nVarChar(1), AMT int, Location nVarChar(50))
insert into @a(mDate, MTime, ACCCT, Atm, AC, AMT, Location)
select N'7/11/16',  1040, 5555,   '',    'C',   0,  'DUNKIN #3'          
union all
select N'7/11/16',  1105, 5555,   '#1',  'W',   -20,  ''          
union all
select N'7/11/16',  1105, 5555,   '',    'С',   0,  'ATM'          
union all
select N'7/11/16',  1105, 5555,   '',    'С',   0,  'DUNKIN #3'          
union all
select N'7/12/16',  2141, 5555,   '',    'С',   0,  'BP#9'          
union all
select N'7/19/16',  1025, 5555,   '#2',  'W',   -40,  ''          
union all
select N'7/19/16',  1025, 5555,   '',  'C',   0,  'ATM'          
union all
select N'7/19/16',  1025, 5555,   '',  'C',   0,  'SBUCKS #1'          
union all
select N'7/21/16',  2102, 5555,   '',  'C',   0,  ''          

declare @b table(mDate Date, MTime int, ACCCT int, Atm nVarChar(2), AC nVarChar(1), AMT int, mTran nVarChar(50), Location nVarChar(50))


declare @mDate Date, @MTime int, @ACCCT int, @Atm nVarChar(2), @AC nVarChar(1), @AMT int, @Location nVarChar(50)

declare @mDate2 Date, @MTime2 int, @ACCCT2 int, @Atm2 nVarChar(2), @AC2 nVarChar(1), @AMT2 int, @mTran2 nVarChar(50), @Location2 nVarChar(50)

declare @Fl int set @Fl = 0

DECLARE @Cur as CURSOR;

set @Cur = CURSOR FORWARD_ONLY FOR
select mDate, MTime, ACCCT, Atm, AC, AMT, Location
from @a
order by mDate, MTime, AMT;

OPEN @Cur  

FETCH NEXT FROM @Cur INTO @mDate, @MTime, @ACCCT, @Atm, @AC, @AMT, @Location
WHILE @@FETCH_STATUS = 0  
BEGIN 
if @Fl = 2 set @mTran2 = @Location
if @Fl = 1 
begin
    set @Location2 = @Location
    insert into @b (mDate, MTime, ACCCT, Atm, AC, AMT, mTran, Location)
    select @mDate2, @MTime2, @ACCCT2, @Atm2, @AC2, @AMT2, @mTran2, @Location2
end

if @Fl = 2 
    set @Fl = 1
else if @Fl = 1 set @Fl = 0

if @AC = 'W' 
begin
    set @Fl = 2
    set @mDate2 = @mDate
    set @MTime2 = @MTime
    set @ACCCT2 = @ACCCT
    set @Atm2 = @Atm
    set @AC2 = @AC
    set @AMT2 = @AMT
end

FETCH NEXT FROM @Cur INTO @mDate, @MTime, @ACCCT, @Atm, @AC, @AMT, @Location

END 

CLOSE @Cur;
DEALLOCATE @Cur;

select * from @b