Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 93
  • Last Modified:

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

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
JimiJ13
Asked:
JimiJ13
1 Solution
 
Saurabh BhadauriaCommented:
your question is not clear to me ..
0
 
Mike EghtebasDatabase and Application DeveloperCommented:
Declare @MAxID int;
Select  @MAxID = Max(ID) From LogTable;
' here you do your update
Select * From LogTable Where ID > @MAxID;
0
 
JimiJ13Author Commented:
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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
JimiJ13Author Commented:
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
 
Mike EghtebasDatabase and Application DeveloperCommented:
-- 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
 
JimiJ13Author Commented:
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
 
Mike EghtebasDatabase and Application DeveloperCommented:
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
 
PortletPaulCommented:
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
 
JimiJ13Author Commented:
Perfect!  Thanks PortletPaul.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now