Identifiants SQL avec plusieurs transactions en 1 jour

J'ai des problèmes même de savoir quoi chercher, donc toutes les suggestions sont appréciées.

J'ai une table avec la structure suivante dans SQL Server:

AccountID UpdatedDate 123 11/1/2017 08:00:00:000 123 11/1/2017 20:00:00:000 123 11/2/2017 08:00:00:000 456 11/1/2017 08:00:00:000 456 11/1/2017 14:00:00:000 456 11/1/2017 20:00:00:000 789 11/1/2017 08:00:00:000 789 11/2/2017 08:00:00:000 

Je souhaite que mes résultats soient définis pour afficher uniquement les loggings dont la transaction est la même date mais à différents moments de la journée, tels que:

  AccountID UpdatedDate 123 11/1/2017 08:00:00:000 123 11/1/2017 20:00:00:000 456 11/1/2017 08:00:00:000 456 11/1/2017 14:00:00:000 456 11/1/2017 20:00:00:000 

Je n'ai pas beaucoup de chance. Toute aide pour un novice en difficulté serait fantastique, merci !!

Ce n'est peut-être pas la solution la plus élégante, mais elle obtiendra les résultats dont vous avez besoin.

 DECLARE @testTable TABLE (AccountID INT, UpdatedDate DATETIME) INSERT INTO @testTable VALUES ('123', '11/1/2017 08:00:00:000'), ('123', '11/1/2017 20:00:00:000'), ('123', '11/2/2017 08:00:00:000'), ('456', '11/1/2017 08:00:00:000'), ('456', '11/1/2017 14:00:00:000'), ('456', '11/1/2017 20:00:00:000'), ('789', '11/1/2017 08:00:00:000'), ('789', '11/2/2017 08:00:00:000'); WITH accounts AS ( SELECT rowid = ROW_NUMBER() OVER ( PARTITION BY AccountID, cast(UpdatedDate AS DATE) ORDER BY UpdatedDate ), AccountID, UpdatedDate, [day] = cast(UpdatedDate AS DATE), [time] = cast(UpdatedDate AS TIME(0)) FROM @testTable ) SELECT DISTINCT ch.AccountID, ch.UpdatedDate FROM ( SELECT accounts.day, accounts.AccountID FROM accounts WHERE rowid > 1 ) c INNER JOIN accounts ch ON ch.day = c.day AND c.AccountID = ch.AccountID 

Merci pour datatables de l'échantillon, Rooben. ned pour les 2 CTE cependant:

 DECLARE @testTable TABLE (AccountID INT, UpdatedDate DATETIME) INSERT INTO @testTable VALUES ('123', '11/1/2017 08:00:00:000'), ('123', '11/1/2017 20:00:00:000'), ('123', '11/2/2017 08:00:00:000'), ('456', '11/1/2017 08:00:00:000'), ('456', '11/1/2017 14:00:00:000'), ('456', '11/1/2017 20:00:00:000'), ('789', '11/1/2017 08:00:00:000'), ('789', '11/2/2017 08:00:00:000'); WITH Counts AS ( SELECT *, COUNT(*) OVER (PARTITION BY AccountID, CONVERT(date,UpdatedDate)) AS C FROM @testTable) SELECT AccountID, UpdatedDate FROM Counts WHERE C > 1; 

Essaye ça

 DECLARE @T TABLE ( AccountId INT, UpdateDate DATETIME ) INSERT INTO @T VALUES (123,'11/1/2017 08:00:00:000'), (123,'11/1/2017 20:00:00:000'), (123,'11/2/2017 08:00:00:000'), (456,'11/1/2017 08:00:00:000'), (456,'11/1/2017 14:00:00:000'), (456,'11/1/2017 20:00:00:000'), (789,'11/1/2017 08:00:00:000'), (789,'11/2/2017 08:00:00:000') ;WITH CTE AS ( SELECT SeqNo = ROW_NUMBER() OVER(PARTITION BY AccountId,CAST(UpdateDate AS DATE) ORDER BY UpdateDate), * FROM @T ) SELECT * FROM CTE C1 WHERE EXISTS ( SELECT 1 FROM CTE WHERE AccountId = C1.AccountId AND SeqNo > 1 AND CAST(UpdateDate AS DATE) = CAST(c1.UpdateDate AS DATE) ) 

Une autre option consiste à utiliser group by et à get un set d'identifiants de count, de paires de dates et à filterr par rapport à cet set.

DECLARE @testTable TABLE (AccountID INT, UpdatedDate DATETIME)

 INSERT INTO @testTable VALUES ('123', '11/1/2017 08:00:00:000'), ('123', '11/1/2017 20:00:00:000'), ('123', '11/2/2017 08:00:00:000'), ('456', '11/1/2017 08:00:00:000'), ('456', '11/1/2017 14:00:00:000'), ('456', '11/1/2017 20:00:00:000'), ('789', '11/1/2017 08:00:00:000'), ('789', '11/2/2017 08:00:00:000'); WITH grouped AS (SELECT AccountID , CAST(UpdatedDate AS DATE) AS UpdatedDateOnly FROM @testTable GROUP BY AccountID, CAST(UpdatedDate AS DATE) HAVING COUNT(*) > 1) SELECT tt.* FROM @testTable tt INNER JOIN grouped g ON g.AccountID = tt.AccountID AND g.UpdatedDateOnly <= tt.UpdatedDate AND tt.UpdatedDate < DATEADD(DAY, 1, g.UpdatedDateOnly);