How to find the all the records which are having multiple status for the same account Id (doesn't matter what status it's belong) on the same Date, doesn't matter about the time, but Date is important
Expected Result should as be mentioned in below table, I should pick the data which are having multiple status on the same day for the same account Id
| AccountId | Status | lastupdatedTimestamp |
|---|---|---|
| 12345 | DISB | 2023-09-07 06:30:30.000 |
| 12345 | SET | 2023-09-07 06:34:30.000 |
| 32341 | REB | 2023-09-07 14:30:30.000 |
| 32341 | D | 2023-09-07 15:31:30.000 |
| 52355 | SET | 2023-09-09 14:30:30.000 |
| 52355 | D | 2023-09-09 15:31:30.000 |
Sample table data :
CREATE TABLE PaymentRecord
(
accountid BIGINT,
Status varchar(10),
lastupdatedTimestamp DATETIME
)
INSERT INTO PaymentRecord (accountid, Status, lastupdatedTimestamp)
VALUES (12345, 'DISB', '2023-09-07 16:30:30.000')
INSERT INTO PaymentRecord (accountid, Status, lastupdatedTimestamp)
VALUES (12345, 'SET', '2023-09-07 16:34:30.000')
INSERT INTO PaymentRecord (accountid, Status, lastupdatedTimestamp)
VALUES (12346, 'D', '2023-09-07 11:30:30.000')
INSERT INTO PaymentRecord (accountid, Status, lastupdatedTimestamp)
VALUES (22341, 'CLR', '2023-09-08 13:30:30.000')
INSERT INTO PaymentRecord (accountid, Status, lastupdatedTimestamp)
VALUES (32341, 'REB', '2023-09-08 14:30:30.000')
INSERT INTO PaymentRecord (accountid, Status, lastupdatedTimestamp)
VALUES (32341, 'D', '2023-09-08 15:31:30.000')
INSERT INTO PaymentRecord (accountid, Status, lastupdatedTimestamp)
VALUES (42325, 'CLR', '2023-09-09 11:30:30.000')
INSERT INTO PaymentRecord (accountid, Status, lastupdatedTimestamp)
VALUES (52355, 'SET', '2023-09-09 14:30:30.000')
INSERT INTO PaymentRecord (accountid, Status, lastupdatedTimestamp)
VALUES (52355, 'D', '2023-09-09 15:31:30.000')
INSERT INTO PaymentRecord (accountid, Status, lastupdatedTimestamp)
VALUES (52355, 'SK', '2023-09-10 16:31:30.000')
I have tried the following , but that did not work based on the expected result.
SELECT DISTICNT
AccountId,
Status,
CONVERT (date, LastUpdatedTimestamp)
FROM
PaymentRecord
WHERE
LastUpdatedTimestamp BETWEEN '2023-09-07' AND '2023-09-11'
ORDER BY
AccountId,
CONVERT (date, LastUpdatedTimestamp),
Status DESC
It took me a while to understand your question.
But with your
WHEREI can not reproduce your wanted result.The concept is easy, make a sub-select where you get the accountid and date of the criteria more than one status at a date and join it to the main query.
fiddle