For all customers aged between 25 to 35 years find what is the net total revenue generated by these consumers in last 30 days of transactions from max transaction date available in the data?
Tried many things but not able to solve this
My code
SELECT
SUM(total_amt) [NET TOTAL REVENUE]
FROM Transactions
INNER JOIN Customer ON Transactions.cust_id = Customer.customer_Id
WHERE tran_date >= DATEADD(day,-30,MAX(tran_date)) and DATEDIFF(YEAR,DOB,tran_date) between 25 and 30
2条答案
按热度按时间h7wcgrx31#
I would recommend window functions to get the maximum date:
Some notes:
DATEDIFF()
does not do what you think it does. It counts the number of Jan 1sts between two dates.DATEADD()
is more accurate.cpjpxq1n2#
Tried to do it through temp tables