Sql 从多个表中选择最大日期

Sql 从多个表中选择最大日期,sql,max,Sql,Max,任何帮助都将不胜感激。我正在尝试从一组表中选择最后一个活动日期。这些表包括输入日期、注释日期、付款日期和索赔日期。我只想返回所有这些日期的最大值。此外,我只想要45天以上没有活动的记录。我目前正在使用以下SQL将所有日期输入,然后使用EXCEL中的计算字段计算出其余日期。是否可以使用SQL来完成这一切 提前谢谢 SELECT xrxTrnLgr.PatId, xrxTrnLgr.Balance, Max(xrxPatNotes.NoteDate) AS 'Max of NoteD

任何帮助都将不胜感激。我正在尝试从一组表中选择最后一个活动日期。这些表包括输入日期、注释日期、付款日期和索赔日期。我只想返回所有这些日期的最大值。此外,我只想要45天以上没有活动的记录。我目前正在使用以下SQL将所有日期输入,然后使用EXCEL中的计算字段计算出其余日期。是否可以使用SQL来完成这一切

提前谢谢

SELECT xrxTrnLgr.PatId, xrxTrnLgr.Balance, 
       Max(xrxPatNotes.NoteDate) AS 'Max of NoteDate', 
       Max(xrxTrnIcf.PostDate) AS 'Max of IcfPostDate', 
       Max(xrxPat.EntryDate) AS 'Entry Date', 
       Max(xrxPat.Coverage) AS 'Coverage', 
       Max(xrxTrnPay.PostDate) AS 'Last Payment'
  FROM  xrxTrnLgr 
  LEFT OUTER JOIN xrxPatNotes ON xrxTrnLgr.PatId = xrxPatNotes.PatId
  LEFT OUTER JOIN  xrxTrnIcf ON xrxTrnLgr.PatId = xrxTrnIcf.PatId
  LEFT OUTER JOIN xrxPat ON xrxTrnLgr.PatId = xrxPat.PatId
  LEFT OUTER JOIN xrxTrnPay ON xrxTrnLgr.PatId = xrxTrnPay.PatId
  GROUP BY xrxTrnLgr.PatId, xrxTrnLgr.Balance
  HAVING (xrxTrnLgr.Balance>$.01)

我认为这可以在SQL中实现:

select t.patid, t.balance,
       max(case when which = 'note' then thedate end) as note,
       max(case when which = 'post' then thedate end) as post,
       max(case when which = 'entry' then thedate end) as entry,
       max(case when which = 'coverage' then thedate end) as coverage,
       max(case when which = 'lastPayment' then thedate end) as lastPayment
from xrxTrnLgr t left join
     ((select patid, notedate as thedate, 'note' as which
       from xrxPatNotes
      ) union all
      (select patid, postdate, 'post'
       from xrxtrnIcf
      ) union all
      (select patid, EntryDate, 'entry'
       from xrxPat
      ) union all
      (select paid, Coverage, 'coverage'
       from xrxPat.Coverage
      ) union all
      (select patid, PostDate, 'LastPayment'
       from xrxTrnPay.PostDate
      ) 
    ) d
    on t.patid = d.patid
group by t.patid, t.balance
having min(now() - thedate) >= 45

您发布的查询有什么问题?哪种SQL方言?mySQL、Microsoft SQL、Oracle等?它是Microsoft SQL。我正在查询SQL server 2008中的数据。我尝试了上面的代码,但没有成功。