Hema
Hema

Reputation: 135

How to compare two rows against a column in a sql table?

Here's the Table:

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

Desired Result should be to track date changes against the Month column as below:

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

This query is not working. Please help. Thanks in advance.

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

Upvotes: 0

Views: 48

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269773

You seem to want a simple aggregation:

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;

Upvotes: 2

Related Questions