question

David 2 1 avatar image
David 2 1 asked

How To Split And Stagger Row Results

Hi all, Is it possible in TSQL 2000 to split and stagger a rows result so that a couple of table columns show on the preceding row? For example: When I query the log table normally I get: CUST STAFF SAVED PRIORITY CREATED AREA ----------------------------------------- 101 A Y 1 2015-08-01 NORTH 101 A Y 3 2015-08-01 SOUTH 101 C N 3 2015-08-04 EAST 101 B Y 1 2015-08-02 WEST However I'd like the CREATED and AREA colum results to shift up one row so the result set would be: CUST STAFF SAVED PRIORITY CREATED AREA ----------------------------------------- 101 A Y 1 2015-08-01 SOUTH 101 A Y 3 2015-08-04 EAST 101 C N 3 2015-08-02 WEST 101 B Y 1 NULL NULL Anyone know if this would be possible? TIA
sql-server-2000tsqlrowresults
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
what determines the order of the data?
0 Likes 0 ·
David 2 1 avatar image David 2 1 commented ·
Sorry I should have mentioned that it's a log table so the order can be determined from the CREATED column which is actually date time. I just made it look like a date column here for simplicity.
0 Likes 0 ·

1 Answer

·
Kev Riley avatar image
Kev Riley answered
There are a couple of ways of implementing the LAG/LEAD functions in SQL 2000. Here's one using correlated sub-queries select CUST, STAFF, SAVED, PRIORITY, CREATED = (select top 1 CREATED from logtable lt2 where lt2.CREATED > lt1.CREATED order by lt2.CREATED), AREA = (select top 1 AREA from logtable lt2 where lt2.CREATED > lt1.CREATED order by lt2.CREATED), from logtable lt1 order by lt1.CREATED
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

David 2 1 avatar image David 2 1 commented ·
Genius!! Many thanks for your help. Strangely I had to reverse the '>' to be '
0 Likes 0 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
Yeah I probably mocked up my data a different way, but at least you got the idea
0 Likes 0 ·
David 2 1 avatar image David 2 1 commented ·
It's more probable the way I was trying to describe what I was after. Amazing help as always. Thank you.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.