sql - Count records with a criteria like "within days" -
i have table below on sql.
orderid account ordermethod orderdate dispatchdate dispatchmethod 2145 qaz 14 20/3/2011 23/3/2011 2 4156 aby 12 15/6/2011 25/6/2011 1
i want count records have reordered 'within 30 days' of dispatch date dispatch method '2' , ordermethod '12' , has come same account.
i want ask if can achieved 1 query or need create different tables , in stages think wll have now? please can code/query?
many t
try following, replacing [tablename] name of table.
select count(originalorders.orderid) [total_orders] [tablename] originalorders inner join [tablename] reorders on originalorders.account = reorders.account , originalorders.orderdate < reorders.orderdate , datediff(day, originalorders.dispatchdate, reorders.orderdate) <= 30 , reorders.dispatchmethod = '2' , reorders.ordermethod = '12';
by using inner join you'll sure grab orders meet criteria.
linking 2 tables (which same table using aliases) make sure orders under same account counted.
results join further filtered based on criteria mentioned requiring orders have been placed within 30 days of dispatch date of previous order.
Comments
Post a Comment