Friday, March 30, 2012

Help on the last updated date

Hi,
I'm trying to retrieve data from the last updated date. For example, when
job runs after long holiday, it will look for the last updated date and
retrieve the data. how would I achieve this?
I tried Max(invoicedate), the query is always timed out.
Please help,
Thanks,
SarahSELECT TOP 1 columns FROM table ORDER BY invoicedate DESC
Do you have an index on invoicedate? If not, then on a large table I really
wouldn't be surprised to see a timeout in either case.
"SG" <sguo@.coopervision.ca> wrote in message
news:u3bACnb%23FHA.3496@.TK2MSFTNGP15.phx.gbl...
> Hi,
> I'm trying to retrieve data from the last updated date. For example, when
> job runs after long holiday, it will look for the last updated date and
> retrieve the data. how would I achieve this?
> I tried Max(invoicedate), the query is always timed out.
> Please help,
> Thanks,
> Sarah
>|||Hi Aaron,
Thanks for your quick response. Actually what I need to do is that I like to
have this max updated date to be a critiria to retrieve data from a few
tables, can I do this?
Thanks,
Sarah
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:%23Vbyd9b%23FHA.1248@.TK2MSFTNGP14.phx.gbl...
> SELECT TOP 1 columns FROM table ORDER BY invoicedate DESC
> Do you have an index on invoicedate? If not, then on a large table I
> really wouldn't be surprised to see a timeout in either case.
>
> "SG" <sguo@.coopervision.ca> wrote in message
> news:u3bACnb%23FHA.3496@.TK2MSFTNGP15.phx.gbl...
>|||> Thanks for your quick response. Actually what I need to do is that I like
> to have this max updated date to be a critiria to retrieve data from a few
> tables, can I do this?
Probably, but without better requirements, I can't tell you how.
http://www.aspfaq.com/5006|||Hi Aaron,
I have a table has sales history, it will be updated every day. I like to
get last day's sales on my report. I used DATEADD(d, -1, GETDATE()), it
works when there is no long wend or holiday. But I like to get a solution
to refine this to look for the last updated date. For example: Last day
sales was on Dec 23, 2005, and when I come back from holiday on Dec 28,2005,
I like my program to run to retrieve the data of Dec 23, not Dec 27.
Select * from sales where invoicedate=DATE(d,-1,GETDATE()).
Can I do this?
Thanks,
Sarah
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:OIFlDRc%23FHA.1484@.tk2msftngp13.phx.gbl...
> Probably, but without better requirements, I can't tell you how.
> http://www.aspfaq.com/5006
>|||SELECT * --specify your columns; avoid the use of * in production
FROM sales
WHERE invoicedate = (SELECT MAX(InvoiceDate) FROM sales)
Be careful, though. If you get one order on the last day, and all of
your other orders show up on the day before, you'll have an awful empty
report.
Stu

No comments:

Post a Comment