Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 get Last Modified Date for a record

Author  Topic 

sqldoubt
Starting Member

17 Posts

Posted - 2010-06-16 : 17:20:01
How to get Last modified date for a record in a table
I have declare it
CREATE TABLE [dbo].[AdCRMMessageprocess_ErrorLog](
[ErrorLogId] [int] IDENTITY(1,1) NOT NULL,
[OrderId] [int] NULL,
[OrderItemId] [int] NULL,
[ErrorMessage] [varchar](max) NULL,
[ErrorCode] [varchar](10) NULL,
[RetryCount] [int] NULL,
[MessageStatus] [int] NULL,
[LastProcessedOn] [datetime] NULL,
[CreatedOn] [datetime] NULL

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-06-16 : 17:23:05
something like this
SELECT * FROM 
(
SELECT ROW_NUMBER() OVER(PARTITION BY OrderId ORDER BY LastProcessedOn DESC) AS SEQ, * FROM [dbo].[AdCRMMessageprocess_ErrorLog]
) T
WHERE T.SEQ = 1
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-06-16 : 17:29:16
oye...what's the obsession with ROW_NUMBER

M$ messed up by giving in to the Oracle Denizens...

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-06-16 : 17:31:30
quote:
Originally posted by X002548

oye...what's the obsession with ROW_NUMBER

M$ messed up by giving in to the Oracle Denizens...

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam






I just thought about it. Most of the solutions I provide involves ROW_NUMBER(). Never realized it till now . Got to move out out it for sure.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-06-16 : 17:38:58
quote:
M$ messed up by giving in to the Oracle Denizens...
Not exactly, it's part of a suite of windowing functions, and they are pretty damn cool. Once you find a good example you'll get it and wonder how you did without them.

Here's a nice number generator:
;with 
a(n) as (select 0 union all select 0 union all select 0),
b(n) as (select 0 from a x cross join a y cross join a z),
c(n) as (select 0 from b x cross join b y cross join b z)
select row_number() over (order by n) n from c
Put that into a view and bingo: a numbers/tally table. Uses no storage, is damn fast, and is very easy to expand to any number of rows.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-17 : 03:12:41
quote:
Originally posted by X002548

oye...what's the obsession with ROW_NUMBER

M$ messed up by giving in to the Oracle Denizens...

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam






Have you read this?
http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2010-06-17 : 03:17:32
Holy smokes! That was an awesome query rob! With some minor modifications it'll give you all the numbers you need:

;with
a(n) as (select 0 union all select 0 union all select 0),
b(n) as (select 0 from a x cross join a y cross join a z),
c(n) as (select 0 from b x cross join b y cross join b z),
d(n) as (select 0 from c x cross join c y),
e(rownumber) as (select row_number() over (order by n) n from d)
select * from e where rownumber <= 10000

EDIT: the 10k can be changed to something pretty big if you need to...haven't tried to figure out how much though.

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page
   

- Advertisement -