Solved

How to query the Last Transaction Per Item based on Identity Column?

Posted on 2015-02-03
9
86 Views
Last Modified: 2015-02-04
I have a table with 5 columns that looks like as shown below. As I update the balance according to the RefNo the ID is incrementing.  How can I query them so that I can only get the last transaction pre RefNo?  

IdentityColumn (ID)      RefNo      Action      Status      Balance
12      3524AQSS25      A      OS      1500
14      HSGSTT152      A      OS      30000
13      32LJHHF455      A      OS      32500
16      3524AQSS25      A      PP      1000
17      HSGSTT152      A      FS      0
20      32LJHHF455      A      PP      20000
24      3524AQSS25      A      NS      500
31      32LJHHF455      A      PP      10000
                        
I'm hoping to extract this output from the above data                         
                        
Last Transaction Per RefNo                        
17      HSGSTT152      A      FS      0
24      3524AQSS25      A      NS      500
31      32LJHHF455      A      PP      10000


Any help would be greatly appreciated.
0
Comment
Question by:JimiJ13
9 Comments
 
LVL 12

Expert Comment

by:Saurabh Bhadauria
ID: 40587990
your question is not clear to me ..
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40588010
Declare @MAxID int;
Select  @MAxID = Max(ID) From LogTable;
' here you do your update
Select * From LogTable Where ID > @MAxID;
0
 

Author Comment

by:JimiJ13
ID: 40588019
Saurabh Bhadauria,

In my sample, I have only 3 RefNo (could be many) just to illustrate what I want to get.  When I query I want to get only the last transaction of each RefNo.

I hope that makes sense.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:JimiJ13
ID: 40588022
eghtebas,

Thanks for the quick response.  I hope you are not trying to give me the top 1. Anyway, I will try it and come back to you.


Thanks.
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40588027
-- There is no top 1; put the following in a new query screen and run it after you do the necessary adjustments to it

Declare @MAxID int;                                                
Select  @MAxID = Max(ID) From LogTable;      -- capture the max ID that exists
GO
' here you do your update                                  -- put your update code here
GO
Select * From LogTable Where ID > @MAxID; -- Select fields you want to see

Change  LogTable to whatever you have.
0
 

Author Comment

by:JimiJ13
ID: 40588035
eghtebas,

Using your suggested code I got nothing.

If I do like this, I got the top 1.
Select * From LogTable Where ID = @MAxID;

I need to get the last transaction per RefNo. If I have 20 RefNo, I must get 20 as well.


Thanks.
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40588036
Did you run this:

Declare @MAxID int;                                                
Select  @MAxID = Max(ID) From LogTable;


Select  @MAxID    -- <-- I tested this in SQL Server, it gave me the max ID before update which will be used later in

Select * From LogTable Where ID > @MAxID


If it works form me, there is no reason why it shouldn't for you.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40588148
row_number() works very well for finding the most recent rows.

select
*
from (
          select
          *
          , row_number() over (partition by RefNo order by id DESC) as rn
          from LogTable
          ) as derived
where rn = 1

note I have ordered by ID DESC, there may be other columns you prefer to order by (e.g. date_created).
0
 

Author Closing Comment

by:JimiJ13
ID: 40588366
Perfect!  Thanks PortletPaul.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question