SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get last transaction from multiple groups of records


Get last transaction from multiple groups of records

Author
Message
Lrobinson 93181
Lrobinson 93181
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 Visits: 140
I've gotten a little further

Step 1 query gets me this:
c_iD1 c_rep c_Date
11111 23 2010-09-06
11111 25 2010-10-06

In step 2 I need to look at different data from the same table and find records where c_iD1 = c_iD AND
my "last_date" is < c_Date in order to return:

c_iD1 c_rep1 c_Date c_rep last_dt
11111 23 2010-09-06 25 2010-09-03
11111 25 2010-10-06 28 2010-10-02


I did not include the column "last_dt" in the create #mytable code simply to keep the first step simple, but basically, it is selected from #mytable in step 2 and compared to c_Date from ther first query.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25994 Visits: 17528
Lrobinson 93181 (8/6/2013)
I've gotten a little further

Step 1 query gets me this:
c_iD1 c_rep c_Date
11111 23 2010-09-06
11111 25 2010-10-06

In step 2 I need to look at different data from the same table and find records where c_iD1 = c_iD AND
my "last_date" is < c_Date in order to return:

c_iD1 c_rep1 c_Date c_rep last_dt
11111 23 2010-09-06 25 2010-09-03
11111 25 2010-10-06 28 2010-10-02


I did not include the column "last_dt" in the create #mytable code simply to keep the first step simple, but basically, it is selected from #mytable in step 2 and compared to c_Date from ther first query.


Well if you want my help you have to provide all of the info. Please post ddl with all the columns and the correct data that will match that output. I am willing to help but we just keep going around in circles.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search