You have a table called with Cust_ID, Order_Date, Order_ID, Tran_Amt. How would you select the top 100 customers with the highest spend over a year-long period?
In Oracle:-
SELECT cust_id,total_spend FROM (SELECT cust_id, sum(tran_amount) total_spend FROM customer
WHERE ORDER_DATE >= add_months(sysdate,-12)
GROUP BY cust_d
ORDER BY 2 desc
) WHERE rownum <101;
SELECT cust_id,total_spend FROM (SELECT cust_id, sum(tran_amount) total_spend FROM customer
WHERE ORDER_DATE >= add_months(sysdate,-12)
GROUP BY cust_d
ORDER BY 2 desc
) WHERE rownum <101;
Answers ( 2 )
SELECT Cust_id, order_date, order_id, tran_amt
FROM Orders
WHERE DATEDIFF(day, order_date, get_date()) > 365
ORDER BY tran_amt DESC;
DATEDIFF – to calculate the difference between two date values
GETDATE() returns the current/today’s date
It is Getdate(), not get_date()
In Oracle:-
SELECT cust_id,total_spend FROM (SELECT cust_id, sum(tran_amount) total_spend FROM customer
WHERE ORDER_DATE >= add_months(sysdate,-12)
GROUP BY cust_d
ORDER BY 2 desc
) WHERE rownum <101;
In Oracle:-
SELECT cust_id,total_spend FROM (SELECT cust_id, sum(tran_amount) total_spend FROM customer
WHERE ORDER_DATE >= add_months(sysdate,-12)
GROUP BY cust_d
ORDER BY 2 desc
) WHERE rownum <101;