Author |
Topic |
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2009-06-18 : 09:07:27
|
Hi GuysI have the following problem:I have the following two tables. One shows when a customer has contacted us, and the second shows any orders that been made by the customer. (Both tables have been scaled down)ContactTable (Scaled Down):CusID,ContactDate1,2009-01-011,2009-01-021,2009,01,052,2009-01-023,2009-01-033,2009-01-104,2009-01-024,2009-01-044,2009-01-05OrdersTable (Scaled Down):OrderID, CusID,OrderDate1,1,2009-12-312,1,2009-01-023,1,2009-01-034,2,2009-01,205,3,2009-01-026,3,2009-01-117,4,2008-12-318,4,2009-01-099,4,2009-01-1110,4,2009-04-0511,4,2009-01-06The thing that I am trying to achieve (but have been stuck on for quite some time now) is when a customer has contacted I want to be able to show the next order they have placed after that contact.So the desired result would be something like below:CusID,ContactDate,OrderID,OrderDate1, 2009-01-01, 2, 2009-01-021, 2009-01-02, 3, 2009-01-031, 2009,01,05, NULL, NULL2, 2009-01-02, 4, 200-01-203, 2009-01-03, 6, 2009-01-113, 2009-01-10, 6, 2009-01-114, 2009-01-02, 9, 2009-01-114, 2009-01-04, 9, 2009-04-054, 2009-01-05, 9, 2009-01-06I’ve tried using the ROW_NUMBER and MIN functions but still had no luck.Anyone have an ideas on how I can achieve this???Thanking you in advance. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-18 : 09:40:23
|
Yes. Visakh will shortly show you a method with CROSS APPLY. E 12°55'05.63"N 56°04'39.26" |
 |
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2009-06-18 : 10:15:11
|
OK, Thanks |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-06-18 : 13:09:03
|
Apparently Visakh is slacking.. :)Here is one method usng CROSS APPLY:DECLARE @Contact TABLE (CusID INT ,ContactDate DATETIME)INSERT @ContactSELECT 1, '2009-01-01'UNION ALL SELECT 1, '2009-01-02'UNION ALL SELECT 1, '2009-01-05'UNION ALL SELECT 2, '2009-01-02'UNION ALL SELECT 3, '2009-01-03'UNION ALL SELECT 3, '2009-01-10'UNION ALL SELECT 4, '2009-01-02'UNION ALL SELECT 4, '2009-01-04'UNION ALL SELECT 4, '2009-01-05'DECLARE @Orders TABLE (OrderID INT, CusID INT, OrderDate DATETIME)INSERT @OrdersSELECT 1,1,'2009-12-31'UNION ALL SELECT 2,1,'2009-01-02'UNION ALL SELECT 3,1,'2009-01-03'UNION ALL SELECT 4,2,'2009-01-20'UNION ALL SELECT 5,3,'2009-01-02'UNION ALL SELECT 6,3,'2009-01-11'UNION ALL SELECT 7,4,'2008-12-31'UNION ALL SELECT 8,4,'2009-01-09'UNION ALL SELECT 9,4,'2009-01-11'UNION ALL SELECT 10,4,'2009-04-05'UNION ALL SELECT 11,4,'2009-01-06'SELECT C.CusID, C.ContactDate, D.OrderID, D.OrderDateFROM @Contact AS CCROSS APPLY ( SELECT TOP 1 OrderID, OrderDate FROM @Orders AS O WHERE O.CusID = C.CusID AND O.OrderDate > C.ContactDate ORDER BY OrderDate ASC ) AS D NOTE: I think the data is slightly off for the first Order date?? |
 |
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2009-06-19 : 06:38:16
|
Thats GreatIs there any way of showing the NULL values to, Ie. Displaying contacts even if an order hasn't been made after the contact date.Thanks |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-06-19 : 12:28:24
|
Yeah, if you change the CROSS APPLY to an OUTER APPLY that will get you the null results. However, your sample data does not produce a NULL. The first row in in the Order data is 12/31/09 so that shows up where you indicate you want a NULL value in your result set. Maybe that is just a typo on the date..? |
 |
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2009-06-20 : 08:26:38
|
HeyThanks for that!Yes it was a typo. |
 |
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-06-20 : 11:51:02
|
Visakh pro in cross apply?hehe |
 |
|
|
|
|