如何将两行与sql表中的列进行比较?

时间:2017-05-18 23:37:01

标签: sql sql-server

这是表:

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   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

1 个答案:

答案 0 :(得分: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;