SQL 2008 R2
Hi All,
If I have a query like so:
select contractno, Address1,dateJoined
from tableTest
where datejoined between 2012-12-01 and 2015-12-31
and my results are:
Contractno,address1,datejoined
387, johnroad, 2013-06-01
267, bobroad, 2015-12-01
267, bobroad, 2015-12-01
578, ellenroad, 2012-12-24 (edit - this date was wrong)
Columns Contractno,address1,datejoined should be unique, but because of poor data, the result has duplicates (rows,2 and 3). There is another field in the table "DatelastTrans" - which is a datefield. In the event of a duplicate, I would like to pull out the max of "DatelastTrans" - this would then give me unique results.
How can I write a query where if there are duplicates, then look at datelasttrans and get the max record?
So my expected result would be:
Contractno,address1,datejoined
387, johnroad, 2013-06-01
267, bobroad, 2015-12-01
578, ellenroad, 2012-12-24 (edit - this date was wrong)
I am not sure if I can do this in one query?
Thanks
Michael
Hi All,
If I have a query like so:
select contractno, Address1,dateJoined
from tableTest
where datejoined between 2012-12-01 and 2015-12-31
and my results are:
Contractno,address1,datejoined
387, johnroad, 2013-06-01
267, bobroad, 2015-12-01
267, bobroad, 2015-12-01
578, ellenroad, 2012-12-24 (edit - this date was wrong)
Columns Contractno,address1,datejoined should be unique, but because of poor data, the result has duplicates (rows,2 and 3). There is another field in the table "DatelastTrans" - which is a datefield. In the event of a duplicate, I would like to pull out the max of "DatelastTrans" - this would then give me unique results.
How can I write a query where if there are duplicates, then look at datelasttrans and get the max record?
So my expected result would be:
Contractno,address1,datejoined
387, johnroad, 2013-06-01
267, bobroad, 2015-12-01
578, ellenroad, 2012-12-24 (edit - this date was wrong)
I am not sure if I can do this in one query?
Thanks
Michael