我希望从表格(12/3/09)中获取最后一个日期,并在字段中显示每个Well,自上次日期开始。
Field, Well, TestDate, Amount X, A, 12/1/09, 500 Y, D, 12/1/09, 400 Y, E, 12/1/09, 300 Y, F, 12/2/09, 50 X, B, 12/2/09, 40 Z, G, 12/2/09, 30 X, C, 12/3/09, 512 Y, D, 12/3/09, 425 Z, G, 12/3/09, 31
SELECT Field, Well, Amount, Last(Date) as LastDate
FROM table1
GROUP BY Field, Well, Amount
ORDER BY Last(Date), Field, Well
产生这个:
X, C, 12/3/09, 512 Y, D, 12/3/09, 425 Z, G, 12/3/09, 31
期望的结果:
X, A, 12/1/09, 500 X, B, 12/2/09, 40 X, C, 12/3/09, 512 Y, D, 12/3/09, 425 Y, E, 12/1/09, 300 Y, F, 12/2/09, 50 Z, G, 12/3/09, 31
非常感谢任何帮助。
答案 0 :(得分:1)
一个选项:从子查询中获取所需日期并将其连接回主表:
SELECT Field, Well, LastDate, Amount from table1 t1
join (
SELECT MAX(Date) as LastDate From Table1 Group by Well
) t2 on t1.Date = t2.LastDate