Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Syntax help
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01400647
Message ID:
01400696
Views:
45
>Good point...closest down. Capture Date must be less than or equal to PO Date.
DECLARE @TableA TABLE (Edp int, PODate datetime)
DECLARE @TableB TABLE (Edp int, CaptureDate datetime, Alert varchar(20))
INSERT INTO @TableA VALUES(123,'20090503')
INSERT INTO @TableA VALUES(456,'20090501')


INSERT INTO @TableB VALUES(123,'20090510','BiLow')
INSERT INTO @TableB VALUES(123,'20090503','Low')
INSERT INTO @TableB VALUES(123,'20090429','BO')
INSERT INTO @TableB VALUES(456,'20090428','Low')
INSERT INTO @TableB VALUES(456,'20090425','Zero')

SELECT TableA.Edp,
       TableA.PODate,
       TableB.CaptureDate,
       TableB.Alert
FROM @TableA TableA
INNER JOIN (SELECT TableB.Edp,
                   MAX(TableB.CaptureDate) AS CaptureDate
            FROM @TableB TableB
            INNER JOIN @TableA TableA ON TableB.Edp = TableA.Edp AND
                                         TableB.CaptureDate <= TableA.PODate
            GROUP BY TableB.Edp) TblDat ON TableA.Edp = TblDat.Edp AND
                                          TableA.PODate >= TblDat.CaptureDate
INNER JOIN @TableB TableB ON TableA.Edp = TableB.Edp AND
                             TableB.CaptureDate = TblDat.CaptureDate
Not properly tested!
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform