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
 How to get the nearest date

Author  Topic 

Villanuev
Constraint Violating Yak Guru

478 Posts

Posted - 2011-10-25 : 21:45:18
Guys,

I have this scripts but when I run i have some problem getting the nearest date of TABLE1 "Receiptdate" to TABLE2 "Shipdate"
some of the records are correct but i notice there are some records are not correct especially like this sample below.
can you modify my script. Thanks.

my script getting this result "2011-02-02" to "2011-08-16"
The correct result should be "2011-05-24" to "2011-08-16"


here is the script:
Select
t2.ESN,
t2.shipdate,
min(t1.receiptdate) as AsiaReceiptdate
--Into #Data
From TABLE1 as t2
Left Outer Join TABLE2 as t2
On t1.esn = t2.esn
and t2.receiptdate > t1.Shipdate
where
(
t2.Shipdate between('2011-01-01 00:00:00') and ('2011-09-30 23:59:59')
and
t2.type_id = 10
)
Group by t2.ESN, t2.shipdate
order by asiareceiptdate


TABLE1
---------------------
ESN--RECEIVEDATE--TYPE
---------------------
123--2011-08-16--10


TABLE2
---------------------
ESN--SHIPDATE
--------------------
123--2011-02-02
123--2011-05-24
123--2011-09-03

Note:
How to get all my post here in SQLteam site.
I will check some of my previous post.

Regards,

JOV

Villanuev
Constraint Violating Yak Guru

478 Posts

Posted - 2011-10-25 : 23:13:42
I modify my scripts and given me the correct result.
what is your thoughs on my scripts. thanks.
Select
t1.ESN,
t1.Receivedate,
min(t2.shipdate) as shipdate
--Into #Data
From TABLE1 as t1
Left Outer Join TABLE2 as t2
On t1.esn = t2.esn
and t2.shipdate < t1.receivedate
where
(
t1.receievdate between('2011-01-01 00:00:00') and ('2011-09-30 23:59:59')
and
t2.type_id = 10
)
Group by t1.ESN,
t1.receivedate
order by t1.receivedate

regards,

JOV
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-26 : 01:18:28
what you're doing is effectively a hidden rbar triangular join

see

http://www.sqlservercentral.com/articles/T-SQL/61539/

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-26 : 01:22:38
i think you would be better off implementing it as below

Select
t1.ESN,
t1.Receivedate,
t2.shipdate
From TABLE1 as t1
cross apply (select min(t2.shipdate) as shipdate
from TABLE2
where t1.esn = esn
and type_id = 10
and shipdate < t1.receivedate
)as t2
where t1.receievdate >='2011-01-01'
t1.receievdate <'2011-10-01'
order by t1.receivedate


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Villanuev
Constraint Violating Yak Guru

478 Posts

Posted - 2011-10-26 : 03:01:59
Hi Visakh16

waht is the difference between this two format of date.
1. t1.receievdate between('2011-01-01 00:00:00') and ('2011-09-30 23:59:59')
2. t1.receievdate >='2011-01-01' and t1.receievdate <'2011-10-01'


Regards,

JOV
Go to Top of Page

Villanuev
Constraint Violating Yak Guru

478 Posts

Posted - 2011-10-26 : 03:32:37
Hi visakh15

There are some error on the result in shipdate
instead of this date 2011-08-16, the other date was getting from the script (2011-02-11) and also it display like this.


TABLE1
---------------------
ESN--RECEIPTDATE--RMA_TYPE_ID
----------------------------------------
123--2011-09-22--2


TABLE2
---------------------
ESN--SHIPDATE
--------------------
123--2011-08-16
123--2011-02-11


Select
rma.ESN,
rma.modelName,
rma.SupplierId,
es.shipdate,
rma.Receiptdate
From TABLE1 as rma
cross apply (select min(shipdate) as shipdate
from TABLE2
where rma.esn = esn
and rma.receiptdate > shipdate
)as es
where rma.receiptdate >='2011-01-01' and
rma.receiptdate <'2011-10-01'
and rma.rma_type_id=2
order by rma.receiptdate


NOTE:

just replace the min(shipdate) to max(shipdate) and the result is okay. also change the location of rma_type_id.



Thanks,

JOV
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-26 : 12:35:10
quote:
Originally posted by Villanuev

Hi Visakh16

waht is the difference between this two format of date.
1. t1.receievdate between('2011-01-01 00:00:00') and ('2011-09-30 23:59:59')
2. t1.receievdate >='2011-01-01' and t1.receievdate <'2011-10-01'


Regards,

JOV


first case has a slight chance of missing some records if they happen to come after 23:59:59 ( ie in between last second like 23:59:59.213 etc) please keep in mind that datetime field in sql server stores upto 3/1000 of second precision so using second method is faultproof to make sure we're not missing anything

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-10-26 : 13:32:37
See, here I was gonna say, go to your local bar or corner

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


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-26 : 13:43:03
quote:
Originally posted by Villanuev

Hi visakh15

There are some error on the result in shipdate
instead of this date 2011-08-16, the other date was getting from the script (2011-02-11) and also it display like this.


TABLE1
---------------------
ESN--RECEIPTDATE--RMA_TYPE_ID
----------------------------------------
123--2011-09-22--2


TABLE2
---------------------
ESN--SHIPDATE
--------------------
123--2011-08-16
123--2011-02-11


Select
rma.ESN,
rma.modelName,
rma.SupplierId,
es.shipdate,
rma.Receiptdate
From TABLE1 as rma
cross apply (select min(shipdate) as shipdate
from TABLE2
where rma.esn = esn
and rma.receiptdate > shipdate
)as es
where rma.receiptdate >='2011-01-01' and
rma.receiptdate <'2011-10-01'
and rma.rma_type_id=2
order by rma.receiptdate


NOTE:

just replace the min(shipdate) to max(shipdate) and the result is okay. also change the location of rma_type_id.



Thanks,

JOV


i just took it from your original script. if what you want is latest date before the required date you should be using max itself

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-10-26 : 15:00:05
oh, come on..that was funny

9/3 is closer btw

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


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-10-26 : 15:28:46
[code]

CREATE TABLE myTABLE1 (ESN int, RECEIVEDATE date, TYPE int)
CREATE TABLE myTABLE2 (ESN int, RECEIVEDATE date)
GO

INSERT myTable1 (ESN, RECEIVEDATE, TYPE)
SELECT 123, '2011-08-16', 10

INSERT myTable2 (ESN, RECEIVEDATE)
SELECT 123, '2011-02-02' UNION ALL
SELECT 123, '2011-05-24' UNION ALL
SELECT 123, '2011-09-03'
GO

SELECT * FROM myTABLE1 l1 INNER JOIN myTABLE2 r1 ON l1.ESN = r1.ESN
WHERE EXISTS(SELECT l.ESN
FROM myTABLE1 l
INNER JOIN myTABLE2 r
ON l.ESN = r.ESN
GROUP BY l.ESN
HAVING MIN(ABS(DATEDIFF(dd,l.RECEIVEDATE, r.RECEIVEDATE))) = ABS(DATEDIFF(dd,l1.RECEIVEDATE, r1.RECEIVEDATE))
)
GO

DROP TABLE myTABLE1, myTABLE2
GO

[/code]


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


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Villanuev
Constraint Violating Yak Guru

478 Posts

Posted - 2011-10-26 : 22:50:36
Thank you guys for the help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-27 : 02:26:55
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-10-27 : 08:48:57
What did you use?

Did you realize you had the wrong closest date? Unless this is "The Price is right" and you're not allowed to go over???



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


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -