Remove Records

Dear Friends
Please help me to modify this query for the remove this kind of records from the out put
DECLARE @cust_id INT;
DECLARE @Amount DECIMAL(18,2);
DECLARE @ReferenceId VARCHAR(100);
DECLARE @Balance DECIMAL(18,2);
DECLARE @BonusBalance DECIMAL(18,2);
DECLARE @desc VARCHAR(MAX);

DROP TABLE IF EXISTS #duplicates;

-- Using ROW_NUMBER() instead of COUNT(1) for better performance
WITH Duplicates AS (
SELECT
ReferenceId,
CeatedDate,
ROW_NUMBER() OVER (PARTITION BY ReferenceId ORDER BY CeatedDate DESC) AS rn
FROM [Transaction]
WHERE CeatedDate > DATEADD(DAY, -2, GETDATE())
AND transactionTypeID = 2
)
SELECT ReferenceId, CeatedDate
INTO #duplicates
FROM Duplicates
WHERE rn > 1;

-- Main query with improved string parsing
SELECT DISTINCT
cus.FirstName,
cus.LastName,
cus.ContactNumber,
cus.EmailAddress,
t.[Description],
t.Amount,
b.SportName,
t.CustomerId,
d.ReferenceId,
b.BetSlipHeaderId
FROM [Transaction] t
INNER JOIN Customer cus ON cus.id = t.CustomerId
INNER JOIN #duplicates d ON d.ReferenceId = t.ReferenceId
AND d.CeatedDate = t.CeatedDate
LEFT JOIN BetSlipItem b ON b.BetSlipHeaderId =
CASE
WHEN t.[Description] LIKE '%-%-%'
THEN TRY_CAST(PARSENAME(REPLACE(t.[Description], '-', '.'), 2) AS INT)
ELSE NULL
END

WHERE b.source in(1,2,3,4)-- b.source<>6--(b.SportName NOT IN ('casino') OR b.SportName IS NULL)
ORDER BY t.Amount;

fyi


Y-Z=0 THEN account in oder so these pattern of records need to remove from that above query

image
need to get this type of records 02 win records only

Since no sample data has been provided, I can only give you an example:

SUM(Amount) OVER (PARTITION BY ReferenceID ORDER BY rn) calculates a so-called "running total." This technique is similar to the usage of the ROW_NUMBER() function.

I can provide further assistance once an example with data is available.

hi

hope this helps

-- Create tables
CREATE TABLE customer
(
id INT PRIMARY KEY,
firstname VARCHAR(100),
lastname VARCHAR(100),
contactnumber VARCHAR(20),
emailaddress VARCHAR(100)
);

CREATE TABLE [transaction]
(
customerid INT,
[description] VARCHAR(max),
amount DECIMAL(18, 2),
referenceid VARCHAR(100),
createddate DATETIME,
transactiontypeid INT
);

CREATE TABLE betslipitem
(
betslipheaderid INT PRIMARY KEY,
sportname VARCHAR(100),
source INT
);

-- Insert sample data
-- Customers
INSERT INTO customer
(id,
firstname,
lastname,
contactnumber,
emailaddress)
VALUES (1,
'John',
'Doe',
'123-456-7890',
'john@example.com'),
(2,
'Jane',
'Smith',
'987-654-3210',
'jane@example.com'),
(3,
'Mike',
'Johnson',
'555-123-4567',
'mike@example.com'),
(4,
'Sarah',
'Williams',
'444-222-3333',
'sarah@example.com'),
(5,
'Alex',
'Brown',
'777-888-9990',
'alex@example.com');

-- Transactions
INSERT INTO [transaction]
(customerid,
[description],
amount,
referenceid,
createddate,
transactiontypeid)
VALUES (1,
'10-2023-45678',
100.00,
'REF-001',
'2023-10-01',
2),
(1,
'10-2023-45678',
150.00,
'REF-001',
'2023-10-02',
2),
(2,
'11-2023-78901',
200.00,
'REF-002',
'2023-10-03',
2),
(3,
'12-2023-12345',
250.00,
'REF-003',
'2023-10-04',
2),
(3,
'12-2023-12345',
300.00,
'REF-003',
'2023-10-05',
2),
(4,
'13-2023-45678',
400.00,
'REF-004',
'2023-10-06',
2),
(5,
'14-2023-78901',
450.00,
'REF-005',
'2023-10-07',
2),
(5,
'14-2023-78901',
500.00,
'REF-005',
'2023-10-08',
2);

-- BetSlipItem
INSERT INTO betslipitem
(betslipheaderid,
sportname,
source)
VALUES (1001,
'Football',
1),
(1002,
'Basketball',
2),
(1003,
'Baseball',
3),
(1004,
'Hockey',
4),
(1005,
'Tennis',
1),
(1006,
'Golf',
2),
(1007,
'Racing',
3),
(1008,
'Boxing',
4),
(1009,
'Soccer',
1),
(1010,
'Volleyball',
2);