Solved

QUERY SEARCHING CLOSEST DATE

Posted on 2006-11-01
5
274 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now