试图从内连接表中拉出MAX Date

时间:2015-01-05 17:59:15

标签: mysql

Select distinct 
      crt.Tariff,  crte.PrintDescription, cre.Element,
      cret.ElementTemplate, cred.EffectiveDate , crtfc.TrafficClass, crec.RateDecimal
From  cabratetariffelement crte
      inner join cabratetariff crt on crte.tariffid = crt.tariffid
      inner join cabratetariffclass crtc on crt.tariffclassid = crtc.tariffclassid
      inner join cabrateelement cre on crte.elementid = cre.elementid
      inner join CABRateElementTemplate cret on cre.ElementTemplateID = cret.ElementTemplateID
      inner join cabrateelementdate cred on crte.elementid = cred.elementid
      inner join cabrateelementcharge crec on cred.elementdateid = crec.elementdateid
      inner join cabratetrafficclass crtfc on crec.trafficclassid = crtfc.trafficclassid

尝试从CABRateElementDate表中获取最新的EffectiveDate .........

1 个答案:

答案 0 :(得分:0)

Select 
      crt.Tariff,  crte.PrintDescription, cre.Element,
      cret.ElementTemplate, max(cred.EffectiveDate) , crtfc.TrafficClass, crec.RateDecimal
From  cabratetariffelement crte
      inner join cabratetariff crt on crte.tariffid = crt.tariffid
      inner join cabratetariffclass crtc on crt.tariffclassid = crtc.tariffclassid
      inner join cabrateelement cre on crte.elementid = cre.elementid
      inner join CABRateElementTemplate cret on cre.ElementTemplateID = cret.ElementTemplateID
      inner join cabrateelementdate cred on crte.elementid = cred.elementid
      inner join cabrateelementcharge crec on cred.elementdateid = crec.elementdateid
      inner join cabratetrafficclass crtfc on crec.trafficclassid = crtfc.trafficclassid
group by crt.Tariff,  crte.PrintDescription, cre.Element,
      cret.ElementTemplate, crtfc.TrafficClass, crec.RateDecimal