Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Updating tables based on next row

Author  Topic 

dferrantino
Starting Member

5 Posts

Posted - 2009-03-04 : 17:25:39
Hi, having a little trouble.

I have a table with a column of text which I need to concatenate. I have a script already that does that job for me, but it puts the concatenated text in another column on the same sheet, as it builds upon itself. For example, if my data is set up as:
a	Hi
x how
x are
x you
b Fine
x thank
x you

It'll update the table as:
a	Hi	Hi
x how Hi how
x are Hi how are
x you Hi how are you
b Fine Fine
x thank Fine thank
x you Fine thank you

I only need the last row of each. I've been tinkering for a few hours trying to figure out how to do it, but I've only been able to flag the first row of each set. The problem I run into is that the row that needs to be flagged doesn't have anything special, because the information I need to look for (column 1 <> 'x') is in the next row.

Currently, I'm trying to tackle it by capturing the variables and inserting them into a new table, but the script I'm working off of uses UPDATE ... SET to modify the original table, and I can't figure out how to read down one table and insert information into another.

I'm looking for a way to read down the table, stick the current row into a set of variables, and then, if one of the columns has what I'm looking for, dump the variables into a new table before they're overwritten and we move to the next row. My table does not have a sequential index, though it would probably help a ton if it did (I'm stumped as to how to add one and my head is beginning to hurt).

Thanks for your help.

malawneh
Starting Member

24 Posts

Posted - 2009-03-04 : 18:48:05
DECLARE @ConcatField VarChar(1000)

SELECT @ConcatField = ISNULL(@ConcatField,'') + ' ' + ColumnB
FROM tblBuild
WHERE ColumnA = 'a'

Michael Alawneh, DBA
Go to Top of Page

dferrantino
Starting Member

5 Posts

Posted - 2009-03-04 : 19:42:40
quote:
Originally posted by malawneh

DECLARE @ConcatField VarChar(1000)

SELECT @ConcatField = ISNULL(@ConcatField,'') + ' ' + ColumnB
FROM tblBuild
WHERE ColumnA = 'a'

Michael Alawneh, DBA



I'm not having a problem with the concatenation, the second part I'm having trouble with. Maybe I can explain it a little better?

The original data was pulled from a flat file and dumped into Excel, split into columns based on the original structure of the file. It's formatted as such: Tenant-Index#-Status-Date-Memo. The Index# is a running index for each tenant, and it's referenced in a separate file (which I also imported, much more easily). The original system only allowed for comments up to 50 characters long, while the current one allows for 500. I've accounted for this in my concatenation script.

The data itself is laid out like this:
tenant1	200000	1	1/31/2007	memo1 line1
tenant1 200001 1 99/31/2007 memo1 line2
tenant1 200002 1 99/31/2007 memo1 line3
tenant1 200003 1 2/08/2007 memo2 line1
tenant1 200004 1 99/08/2007 memo2 line2
tenant2 200000 1 1/04/2005 memo3 line1
tenant2 200001 1 99/04/2005 memo3 line2

...And so on. The best indication that a new memo is starting is that a real date is entered in the 4th column, or alternatively, left(date,2) <> '99' (the date column was imported as nvarchar). There is no indication at all that the current line is the last line of the memo.

What I'm trying to do is take the last line of each group and either dump it into another table or flag it so I can run a SELECT...WHERE on it. Is there a way to find the last line in each group using a SQL query?
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-03-04 : 19:50:09
yes there is/are way(s). Question for you though are the dates in sequential order , for the same tenant could there be identical dates. what constitues a new group, which column field. in your example tenant1 has two bogus dates so both are last lines. Also what is that second column: is that a sequence column?
Go to Top of Page

dferrantino
Starting Member

5 Posts

Posted - 2009-03-04 : 23:54:36
quote:
Originally posted by yosiasz

yes there is/are way(s). Question for you though are the dates in sequential order , for the same tenant could there be identical dates. what constitues a new group, which column field. in your example tenant1 has two bogus dates so both are last lines. Also what is that second column: is that a sequence column?


The dates are in order per tenant (or at least they should be), and a valid date constitutes a new group. The bogus dates are the way the old system signified continuation, there are some records that have 20~25 lines worth of '99' dates.

The second column is a sequence column, but the sequence starts over for each tenant (at 200000), and it isn't necessarily continuous. Occasionally it'll skip a number. Also, you'll see the third column is always '1', because any record that isn't a '1' in the third column is handled differently so I've separated them.
Go to Top of Page

dferrantino
Starting Member

5 Posts

Posted - 2009-03-05 : 09:27:29
To clarify, even if the third column isn't a '1', the sequence in column 2 continues, so there will occasionally be large jumps in the sequence because I'm ignoring those. Regardless though, there are occasionally breaks in the sequence for no particular reason.
Go to Top of Page

dferrantino
Starting Member

5 Posts

Posted - 2009-03-05 : 12:52:29
Figured it out. Was able to use a cursor and two sets of variables to iterate through the table. Takes a bit longer than I'd like, but 19 seconds is far more manageable than my VBA solution would have most likely been.

Final code to pull data, concatenate into a separate column and flag a new group, and then populate a new table with the proper data was:
create table #workingtable (
tcode int not null,
indnum int,
startdate varchar(10),
memo varchar(50),
newind int,
newdate varchar(10),
fullmemo varchar(500),
flag int)

insert into #workingtable (tcode,indnum,memo,startdate)
select tcode,indnum,memo,date from import_data.dbo.ldmanh#text$ where stat = 1 order by tcode,indnum

declare
@fullmemo varchar(500),
@lastt int,
@newind int,
@newdate varchar(10)

select
@fullmemo = '',
@lastt = -1,
@newind = -1,
@newdate = ''

update
#workingtable
set
flag = case
when @lastt <> tcode or len(@fullmemo)>450 or left(startdate,2) <> '99' then 1
else null
end,
@fullmemo = fullmemo = case
when @lastt <> tcode or len(@fullmemo)>450 or left(startdate,2) <> '99' then rtrim(memo)
else @fullmemo + ' ' + rtrim(memo)
end,
@newind = newind = case
when @lastt <> tcode or len(@fullmemo)>450 or left(startdate,2) <> '99' then indnum
else @newind
end,
@newdate = newdate = case
when left(startdate,2) <> '99' then startdate
else @newdate
end,
@lastt = tcode


create table #workingtable2 (
tcode int not null,
indnum int,
startdate varchar(10),
memo varchar(500))

declare crsr cursor fast_forward for (select tcode, newind, startdate, newdate, fullmemo,flag from #workingtable)
declare
@tcode int,
@index int,
@date varchar(10),
@fullmemo2 varchar(500),
@nexttcode int,
@nextindex int,
@nextdate varchar(10),
@nextmemo varchar(500),
@keydate varchar(10),
@flag int
select
@fullmemo2 = '',
@tcode = -1,
@index = -1,
@date = '',
@nexttcode = -1,
@nextindex = -1,
@nextdate = '',
@nextmemo = '',
@keydate = '',
@flag = -1
open crsr
fetch crsr into @nexttcode, @nextindex, @keydate, @nextdate, @nextmemo,@flag
while @@fetch_status = 0
begin
if @flag = 1
begin
insert into #workingtable2(tcode, indnum, startdate, memo) values (@tcode,@index,@date,@fullmemo2)
select @fullmemo2 = @nextmemo, @tcode = @nexttcode, @index = @nextindex, @date = @nextdate
end
else
select @fullmemo2 = @nextmemo, @tcode = @nexttcode, @index = @nextindex, @date = @nextdate
fetch crsr into @nexttcode, @nextindex, @keydate, @nextdate, @nextmemo, @flag
end

close crsr
deallocate crsr

select * from #workingtable2 order by tcode, indnum

go
Only (very minor) issue now is that the first row is (1,1,'',''), but that will be removed when I dump this temp table into my existing table.
Go to Top of Page
   

- Advertisement -