2017-05-18 55 views
0

这里的表:如何比较sql表中的列与两行?

Program  Year Month  Action   Date 
2006060_PR  2017  1   Approval   3/1/2017 
2006060_PR  2017  2   Approval   4/1/2017 
2006060_PR  2017  3   Approval   4/1/2017 
2006060_PR  2017  4   Approval   4/1/2017 
2006060_PR  2017  5   Approval   5/1/2017 

理想的结果应当是对跟踪如下Month列日期改变:

Month EU_Date 
1  3/1/2017 
2  4/1/2017 
5  5/1/2017 

这查询不工作。请帮忙。提前致谢。

Select distinct A.Month, A.Date 
from Table A inner join 
    Table B 
     on A.Program = B.Program 
where A.Year = 2017 and A.Program = '2006060_PR' and 
     A.Action = 'Approval' and A.Date <> B.Date and A.Month < B.Month 

回答

2

你似乎想要一个简单的汇总:

Select A.Date, min(A.Month) 
from Table A 
where A.Year = 2017 and A.Program = '2006060_PR' and 
     A.Action = 'Approval' 
group by A.Date;