2017-05-18 54 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;