Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Syntax help
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Versions des environnements
SQL Server:
SQL Server 2005
Divers
Thread ID:
01400647
Message ID:
01400696
Vues:
46
>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.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform