2016-06-27 56 views
-4

請幫助我,我是新來的SQL需要幫助的SQL查詢 - 如何獲得最近刪除的記錄

能否請你讓我知道如何從下表獲得最近刪除的記錄。在這裏,我需要做一個查詢只對行擁有超過1行相同數目列

我需要的結果是這樣的: -

我需要的結果是這樣的: -

ID  FName  LName  Number   CreateDate 
2  BBBBB  B   111111  06-26-2016 01:18:000 
3  CCCCC  C   333333  06-25-2016 06:10:000 
4  DDDDD  D   444444  06-25-2016 06:10:000 
5  EEEEE  E   555555  06-25-2016 23:10:000 
7  FFFFF  F   777777  06-26-2016 00:01:000 
8  GGGGG  G   888888  06-26-2016 16:01:000 
9  HHHHH  H   999999  06-26-2016 23:01:000 


Create Table Users1 
(
ID int, 
FName varchar (50), 
LName Varchar (50), 
Number varchar(10), 
CreateDate Datetime 
)  

INSERT INTO Users1 Values (1,'AAAA','A','11111','06-25-2016 00:10:765') 
INSERT INTO Users1 Values (2,'AAAA','A','11111','06-26-2016 01:18:000') 
INSERT INTO Users1 Values (3,'CCCC','C','33333','06-25-2016 06:10:000') 
INSERT INTO Users1 Values (4,'DDDD','D','44444','06-25-2016 06:10:000') 
INSERT INTO Users1 Values (5,'EEEE','E','55555','06-25-2016 23:10:000') 
INSERT INTO Users1 Values (6,'CCCC','C','33333','06-25-2016 00:01:000') 
INSERT INTO Users1 Values (7,'FFFF','F','77777','06-26-2016 00:01:000') 
INSERT INTO Users1 Values (8,'GGGG','G','88888','06-26-2016 16:01:000') 
INSERT INTO Users1 Values (9,'HHHH','H','99999','06-26-2016 23:01:000') 
+0

你有歷史/審計表或預定備份 –

+0

沒有我沒有,我只需要從上面的表中得到結果,即Users1 – Kumar

+0

然後我不認爲你可以得到它 –

回答

0
With Users1 As (
SELECT * FROM (
    VALUES 
     (1, 'AAAA', 'A', '11111', '06-25-2016 00:10:765'), 
     (2, 'AAAA', 'A', '11111', '06-26-2016 01:18:000'), 
     (3, 'CCCC', 'C', '33333', '06-25-2016 06:10:000'), 
     (4, 'DDDD', 'D', '44444', '06-25-2016 06:10:000'), 
     (5, 'EEEE', 'E', '55555', '06-25-2016 23:10:000'), 
     (6, 'CCCC', 'C', '33333', '06-25-2016 00:01:000'), 
     (7, 'FFFF', 'F', '77777', '06-26-2016 00:01:000'), 
     (8, 'GGGG', 'G', '88888', '06-26-2016 16:01:000'), 
     (9, 'HHHH', 'H', '99999', '06-26-2016 23:01:000') 
    ) V (ID, FName, LName, Number, CreateDate) 
), Users1WithVersionNumber As (
    Select * 
     , row_number() Over (Partition By Number Order By CreateDate DESC) As VersionNumber 
    From Users1 
) 
Select FName, LName, Number, CreateDate 
From Users1WithVersionNumber 
Where VersionNumber = 1    --< Take the latest version only 
    AND Number In (
    Select Number 
    From Users1WithVersionNumber 
    Where VersionNumber = 2   --< There is at least one other version 
) 
0
SELECT CreateDate,FName,LName,Number FROM Users1 

WHERE CREATEDATE IN(SELECT MAX(CREATEDATE)從用戶1組由數)