Solved

QUERY SEARCHING CLOSEST DATE

Posted on 2006-11-01
5
277 Views
Last Modified: 2008-03-06
Hi, I'm searching the best and simplest way to make the following query:

I've got some SkU with specific dates. you can find multiple row with same sku, but with different date.
I want the closest date from today (getdate), but not greater.

Sample.

ID      SKU      date
1        abc       01/01/2004
2        abc       01/01/2005
3        abc       01/01/2006
4        abc       01/01/2007

I want the row with ID 3, because the date is closest than date's row ID1 and ID 2.

ID 4's date is closest than ID 3's date but is greater than getdate so not good.

Thank you for your help.
0
Comment
Question by:bruno_boccara
5 Comments
 
LVL 28

Expert Comment

by:imran_fast
ID: 17849305
select  max(date)  date from yourtable where date <= getdate()
0
 
LVL 28

Expert Comment

by:imran_fast
ID: 17849315

select A.* from yourtable A
inner join (
select  max(date)  [date] , sku  from yourtable  where date <= getdate() group by sku )B
on A.sku = B.sku and A.[date] = b.[date]
0
 
LVL 9

Expert Comment

by:dduser
ID: 17849319
Select SKU,Min(DateDiff(day,YourDate,GetDate())) from YourTable where DateDiff(day,YourDate,GetDate()) >= 0
Group by SKU

Regards,

dduser
0
 
LVL 8

Accepted Solution

by:
KelvinY earned 500 total points
ID: 17849329
Hi bruno_boccara,

Try

SELECT * FROM SKUTable WHERE SKU = 'abc' AND [DATE] = (SELECT MAX(DATE) FROM SKUTable WHERE SKU = 'abc' AND [DATE] <= GETDATE())

Regards
  Kelvin
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17849343
select top 1 * from dbo.TESTDATE where DT < getdate()
order by DT desc
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

810 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