是否有可以编写到MS-Access SQL语句中的等效[GREATEST()]
1函数?我有一个包含多个日期字段的项目编号表。我想返回一个指定最近日期的输出。实际上,这表明了特定项目的状态。
+------------+------------+------------+-----------+------------+-----------+
| ProjectNum | CondCommit | FirmCommit | FundAgt | Disbursemt | Servicing |
+------------+------------+------------+-----------+------------+-----------+
| 1898 | | 1/30/2008 | 2/21/2008 | 6/18/2008 | 6/21/2010 |
| 1906 | 12/20/2004 | 5/19/2006 | 5/3/2006 | 4/6/2006 | 4/5/2007 |
| 1918 | 3/31/2009 | 11/19/2009 | 3/24/2010 | 12/22/2010 | |
| 1956 | 3/31/2009 | | 3/5/2010 | | |
+------------+------------+------------+-----------+------------+-----------+
我知道我可以使用SWITCH
函数来比较两个日期,但我必须在五个字段中找到最近的日期。理想情况下,我希望输出看起来像这样:
+------------+------------+------------+
| ProjectNum | Status | StatusDt |
+------------+------------+------------+
| 1898 | Servicing | 6/21/2010 |
| 1906 | Servicing | 4/5/2007 |
| 1918 | Disbursemt | 12/22/2010 |
| 1956 | FundAgt | 3/5/2010 |
+------------+------------+------------+
我愿意尝试各种各样的功能,但我想将代码保存在SQL中。如果我必须将输出发送到另一个应用程序进行处理,那么就会破坏使用SQL开始的目的。
答案 0 :(得分:1)
这是您的解决方案:
Status
的表,其中包含一个名为Status
的列。填写独特的状态。table1
您的代码如下:
select
i.ProjectNum
,oo.Status
,i.StatusDt
from (
select
o.ProjectNum
,Max(o.StatusDt) as StatusDt
from (
select
t.ProjectNum
,t.[Status]
,iif(t.[Status] = 'CondCommit',a.CondCommit,
iif(t.[Status] = 'FirmCommit',a.FirmCommit,
iif(t.[Status] = 'FundAgt',a.FundAgt,
iif(t.[Status] = 'Disbursemt',a.Disbursemt,
iif(t.[Status] = 'Servicing',a.Servicing,null))))) as StatusDt
from
(
select
table1.ProjectNum
,Status.Status
from table1, Status
) as t
inner join table1 as a
on t.ProjectNum = a.ProjectNum
) as o
group by o.ProjectNum
) as i
inner join (
select
t.ProjectNum
,t.[Status]
,iif(t.[Status] = 'CondCommit',a.CondCommit,
iif(t.[Status] = 'FirmCommit',a.FirmCommit,
iif(t.[Status] = 'FundAgt',a.FundAgt,
iif(t.[Status] = 'Disbursemt',a.Disbursemt,
iif(t.[Status] = 'Servicing',a.Servicing,null))))) as StatusDt
from
(
select
table1.ProjectNum
,Status.Status
from table1, Status
) as t
inner join table1 as a
on t.ProjectNum = a.ProjectNum
) as oo
on i.ProjectNum = oo.ProjectNum
and i.StatusDt = oo.StatusDt
注意: