Quote:
the requirement says freeze him for 4 transactions in a day and 10 transactions in a month
So something like this?
WITH cteByDay As
(
SELECT
CustomerId,
TransactionDate,
Count(1) As TransactionCount
FROM
YourTable
GROUP BY
CustomerId,
TransactionDate
),
cteByMonth As
(
SELECT
CustomerId,
DateFromParts(Year(TransactionDate), Month(TransactionDate), 1) As TransactionMonth,
SUM(CASE
WHEN TransactionCount > 4 THEN 4
ELSE TransactionCount
END) As TransactionCount
FROM
cteByDay
GROUP BY
CustomerId,
DateFromParts(Year(TransactionDate), Month(TransactionDate), 1)
)
SELECT
CustomerId,
TransactionMonth,
CASE
WHEN TransactionCount > 10 THEN 10
ELSE TransactionCount
END As TransactionCount
FROM
cteByMonth
ORDER BY
CustomerId,
TransactionMonth
;
This will give you a list of months and customer IDs, with the number of transactions for that customer within that month, limited to a maximum of 4 transactions per day, or 10 transactions per month.