I have table as below and I need to delete row as per commented out below...
ID | AccountID | AccountValue| CreatedDate | Comment
===========================================================================
1 | 1 | 2 | 2016-06-13 19:58:47.373 | Delete
2 | 1 | 2 | 2017-06-13 19:58:47.373 | Delete
3 | 1 | 2 | 2018-06-13 19:58:47.373 |
4 | 2 | 3 | 2017-06-13 19:58:47.373 |
5 | 4 | 4 | 2017-06-13 19:58:47.373 | Delete
6 | 4 | 4 | 2018-06-13 19:58:47.373 |
7 | 5 | 2 | 2017-06-13 19:58:47.373 |
Can someone please help on who to achieve this?
I have somehow
CREATE TABLE MyAccounts (
ID int,
AccountID int,
AccountValue varchar(255),
CreatedDate datetime
);
insert into MyAccounts values(1,1,2,'2016-06-13 19:58:47.373')
insert into MyAccounts values(2,1,2,'2017-06-13 19:58:47.373')
insert into MyAccounts values(3,1,2,'2018-06-13 19:58:47.373')
insert into MyAccounts values(4,2,3,'2017-06-13 19:58:47.373')
insert into MyAccounts values(5,4,4,'2017-06-13 19:58:47.373')
insert into MyAccounts values(6,4,4,'2018-06-13 19:58:47.373')
insert into MyAccounts values(7,5,2,'2017-06-13 19:58:47.373')
I know I want to delete this group of data, but want to left 1 row per explained in original question
select
AccountID,
AccountValue
FROM MyAccounts
GROUP BY AccountID, AccountValue--, createddate
having count(*) > 1
This is so that the table become as per below
ID | AccountID | AccountValue| CreatedDate | Comment
===========================================================================
3 | 1 | 2 | 2018-06-13 19:58:47.373 |
4 | 2 | 3 | 2017-06-13 19:58:47.373 |
6 | 4 | 4 | 2018-06-13 19:58:47.373 |
7 | 5 | 2 | 2017-06-13 19:58:47.373 |