| 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 #DataFrom TABLE1 as t2Left Outer Join TABLE2 as t2On 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') andt2.type_id = 10 )Group by t2.ESN, t2.shipdateorder by asiareceiptdateTABLE1---------------------ESN--RECEIVEDATE--TYPE---------------------123--2011-08-16--10TABLE2---------------------ESN--SHIPDATE--------------------123--2011-02-02123--2011-05-24123--2011-09-03Note: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 #DataFrom TABLE1 as t1Left Outer Join TABLE2 as t2On t1.esn = t2.esn and t2.shipdate < t1.receivedatewhere(t1.receievdate between('2011-01-01 00:00:00') and ('2011-09-30 23:59:59') andt2.type_id = 10 )Group by t1.ESN, t1.receivedateorder by t1.receivedateregards,JOV |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
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 belowSelectt1.ESN,t1.Receivedate,t2.shipdate From TABLE1 as t1cross apply (select min(t2.shipdate) as shipdate from TABLE2 where t1.esn = esn and type_id = 10 and shipdate < t1.receivedate)as t2where t1.receievdate >='2011-01-01't1.receievdate <'2011-10-01'order by t1.receivedate ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2011-10-26 : 03:01:59
|
| Hi Visakh16waht 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 |
 |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2011-10-26 : 03:32:37
|
| Hi visakh15There are some error on the result in shipdateinstead 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--2TABLE2---------------------ESN--SHIPDATE--------------------123--2011-08-16123--2011-02-11Select rma.ESN, rma.modelName, rma.SupplierId, es.shipdate, rma.ReceiptdateFrom TABLE1 as rmacross apply (select min(shipdate) as shipdate from TABLE2where rma.esn = esn and rma.receiptdate > shipdate)as eswhere rma.receiptdate >='2011-01-01' and rma.receiptdate <'2011-10-01'and rma.rma_type_id=2order by rma.receiptdateNOTE:just replace the min(shipdate) to max(shipdate) and the result is okay. also change the location of rma_type_id.Thanks,JOV |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-26 : 12:35:10
|
quote: Originally posted by Villanuev Hi Visakh16waht 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-26 : 13:43:03
|
quote: Originally posted by Villanuev Hi visakh15There are some error on the result in shipdateinstead 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--2TABLE2---------------------ESN--SHIPDATE--------------------123--2011-08-16123--2011-02-11Select rma.ESN, rma.modelName, rma.SupplierId, es.shipdate, rma.ReceiptdateFrom TABLE1 as rmacross apply (select min(shipdate) as shipdate from TABLE2where rma.esn = esn and rma.receiptdate > shipdate)as eswhere rma.receiptdate >='2011-01-01' and rma.receiptdate <'2011-10-01'and rma.rma_type_id=2order by rma.receiptdateNOTE: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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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)GOINSERT myTable1 (ESN, RECEIVEDATE, TYPE)SELECT 123, '2011-08-16', 10INSERT myTable2 (ESN, RECEIVEDATE)SELECT 123, '2011-02-02' UNION ALLSELECT 123, '2011-05-24' UNION ALLSELECT 123, '2011-09-03'GOSELECT * 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)) )GODROP TABLE myTABLE1, myTABLE2GO[/code]Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2011-10-26 : 22:50:36
|
| Thank you guys for the help. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-27 : 02:26:55
|
| wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|