SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query Current and Previous record
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bhussey
Starting Member

5 Posts

Posted - 01/17/2013 :  18:03:38  Show Profile  Reply with Quote
Hello, I am trying to create a query that will query any action changes made within the last 30 days. Let's say someone changes their name, I'd like to pull their current name/record and the previous name/record.

This is the query used to pull the current record
select employee, fld_nbr, date_stamp
from hrhistory
Where fld_nbr = '2'
and date_stamp > (sysdate-30)

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 01/17/2013 :  18:36:11  Show Profile  Reply with Quote
You didn't way which version of SQL Sever you are using, but one way is with the ROW_NUMBER() function:
select *
from
(
    select 
        employee, 
        fld_nbr, 
        date_stamp,
        ROW_NUMBER() OVER (PARTITION BY employee ORDER BY date_stamp DESC) AS RowNum
    from 
        hrhistory
    Where 
        fld_nbr = '2'
        and date_stamp > (sysdate-30)
) as t
where
    RowNum IN (1, 2)
Go to Top of Page

bhussey
Starting Member

5 Posts

Posted - 01/17/2013 :  18:42:35  Show Profile  Reply with Quote
I'm using SQL Developer
Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 01/17/2013 :  20:04:21  Show Profile  Reply with Quote
quote:
Originally posted by bhussey

Hello, I am trying to create a query that will query any action changes made within the last 30 days. Let's say someone changes their name, I'd like to pull their current name/record and the previous name/record.

This is the query used to pull the current record
select employee, fld_nbr, date_stamp
from hrhistory
Where fld_nbr = '2'
and date_stamp > (sysdate-30)



Do you want them in 2 separate column? How does output look like
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/17/2013 :  22:38:33  Show Profile  Reply with Quote
quote:
Originally posted by bhussey

Hello, I am trying to create a query that will query any action changes made within the last 30 days. Let's say someone changes their name, I'd like to pull their current name/record and the previous name/record.

This is the query used to pull the current record
select employee, fld_nbr, date_stamp
from hrhistory
Where fld_nbr = '2'
and date_stamp > (sysdate-30)


do you've audit tables to track the changes? or do you add it as a new item in your main table (type 2)?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000