Hi @Gianluca Appetito
Please have a check on this:
create table #test
([user] nvarchar(10),
amount int,
[datetime] datetime
)
insert into #test VALUES
('john' , 245 ,' 2021-12-1 20:21:00 '),
('john' , 143 ,' 2021-12-1 10:20:00 '),
('john' , 763 ,' 2021-12-2 22:15:00 '),
('ted' , 125 ,' 2021-12-1 15:14:00 '),
('ted' , 325 ,' 2021-12-1 18:21:00 '),
('Mike' , 131 ,' 2021-12-1 20:21:00 '),
('Mike' , 115 ,' 2021-12-1 10:21:00 '),
('Mike' , 160 ,' 2021-12-2 20:00:00 '),
('Mike' , 180 ,' 2021-12-2 22:00:00 ')
SELECT * FROM #test;
ALTER TABLE #test ADD identity_no INT;
GO
;WITH CTE AS
(
SELECT [user],SUM(amount)AS amount,CONVERT(DATE,datetime) AS DATE,1 as identity_no
FROM #test
GROUP BY [user],CONVERT(DATE,datetime)
)
INSERT INTO #test ([user],amount,[datetime],identity_no) SELECT * FROM CTE;
DELETE FROM #test WHERE identity_no IS NULL;
ALTER TABLE #test DROP COLUMN identity_no;
SELECT * FROM #test
Output:
Best regards,
LiHong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.