Select Top 10 Sales Amounts Every Month, Every Year SQL Server 2008 -
select tr.createdon, st.be_storecountry, st.be_storelocationcategory, st.be_name storename, br.be_name brand, sum(sd.be_amount) total filteredbe_transaction tr, filteredbe_store st, filteredbe_salesdetails sd, filteredbe_brand br sd.be_itembrand = br.be_brandid , tr.be_storename = st.be_storeid , tr.be_transactionid = sd.be_transactionid , tr.createdon between '1/1/2008' , '1/1/2014' group tr.createdon, st.be_storecountry, st.be_storelocationcategory, st.be_name, br.be_name order tr.createdon desc
this query , returns 286 rows. need modify top 10 sales amount every month, every year. each month of year 2008 2014 must have 10 corresponding results , must max sales amounts. 1 can please?
you use partition
break subsets based on month , year, , select top 10 each such subset result, so:
;with cte ( select tr.createdon, st.be_storecountry, st.be_storelocationcategory, st.be_name storename, br.be_name brand, sum(sd.be_amount) total filteredbe_transaction tr, filteredbe_store st, filteredbe_salesdetails sd, filteredbe_brand br sd.be_itembrand = br.be_brandid , tr.be_storename = st.be_storeid , tr.be_transactionid = sd.be_transactionid , tr.createdon between '1/1/2008' , '1/1/2014' group tr.createdon, st.be_storecountry, st.be_storelocationcategory, st.be_name, br.be_name ), filtered ( select *, row_number() on (partition year(createdon),month(createdon) order total desc) rn cte) select * filtered rn <= 10
Comments
Post a Comment