Problème de requête SQl –

J'ai des problèmes dans la requête:

priceDeatil ProductCode BusinessUnit price DateFrom DateTo 10001 ORB 12.00 12-08-2011 31-09-2015 10001 ORB 21.00 01.08-2011 15-11-2011 10002 ORB 31.00 01.04-2011 15-08-2012 10003 ORB 42.00 01.05-2011 15-08-2012 

Ma question est:

  SELECT BusinessUnit, ProductCode, DateFrom, DateTo, Price FROM WMPriceDetail WHERE (DateFrom < 'Sep 01 2011') AND (DateTo > 'Sep 01 2011' OR DateTo = '') ORDER BY ProductCode 

Ce return

  priceDeatil ProductCode BusinessUnit price DateFrom DateTo 10001 ORB 12.00 12-08-2011 31-09-2015 10001 ORB 21.00 01.08-2011 15-11-2011 10002 ORB 31.00 01.04-2011 15-08-2012 10003 ORB 42.00 01.05-2011 15-08-2012 

Mais ici, productCode 10001 renvoie deux loggings; Cette fois-ci, je veux get la date maximale, c'est-à-dire le 12-08-2011 .

Donc le résultat souhaité devrait être comme ceci:

  priceDeatil ProductCode BusinessUnit price DateFrom DateTo 10001 ORB 12.00 12-08-2011 31-09-2015 10002 ORB 31.00 01.04-2011 15-08-2012 10003 ORB 42.00 01.05-2011 15-08-2012 

S'il vous plaît aidez-moi … Comment écrire une requête pour cette situation?

Merci d'avance

      SELECT BusinessUnit, ProductCode, DateFrom, DateTo, Price FROM (SELECT BusinessUnit, ProductCode, DateFrom, DateTo, Price, rank() over (PARTITION BY ProductCode ORDER BY DateFrom DESC) rank_num FROM WMPriceDetail WHERE (DateFrom < 'Sep 01 2011') AND (DateTo > 'Sep 01 2011' OR DateTo = '') t WHERE rank_num=1 ORDER BY ProductCode 

    Utilisez cette requête:

     select BusinessUnit, ProductCode, DateFrom, DateTo, Price from WMPriceDetail where DateFrom in (select MAX(datefrom) from WMPriceDetail group by ProductCode)