Solved

fetching data from last 5 days

Posted on 2013-05-22
5
175 Views
Last Modified: 2013-05-22
hi guys

i have a table Sale_Region like


sale_Date   region    customer_name

2013-05-19     UK                  Jay
2013-03-1      US                  Jack
2013-05-22     AUS                 Jill
2013-05-21     UK                  Anthony



I want to return three columns max(sale_Date) , sale_Date which is 5 working days before  max(sale_Date),
region from the table

any idea how i can do that?



thanks
0
Comment
Question by:royjayd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

by:royjayd
ID: 39187874
any answers greatly appreciated
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 415 total points
ID: 39187918
Give this a whirl..

SELECT region, customer_name, max(sale_date), DATEADD(dd, -5,  max(sale_date)) as sale_date_minus_five_days
FROM YourTable
GROUP BY region, customer_name
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39187928
'working days'

How does your company define this?  Most companies will have a custom table where each day is a row, and columns for fiscal year/month/week/whatever, working days, etc.

Problem is, everyone has different holdiays, so what's a working day will vary wildly between companies and countries, such that a single SQL function won't cover all situations.
0
 

Author Comment

by:royjayd
ID: 39188014
jim

I think that sql is pretty good and i agree with your point about working days , our users are in US and UK.

thanks.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39188055
Thanks for the grade.  Good luck with your project.  -Jim

btw a TIME table is invaluable imho, for dealing with not only working days, but if your company has a really screwed up fiscal year such as crop year / federal year, and if fiscal months are 4-5-4 instead of straight calendar months.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …

733 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