SQL Top 10 Sales Every Month

帅比萌擦擦* 提交于 2019-12-13 12:15:34

问题


Greeting all. I have a SQL 2008 express database, lets name is tbl_Merchant, similar as following:

Merchant | Sales | Month
Comp.1      100     1
Comp.2      230     1
Comp.3      120     1
Comp.1      200     2
Comp.2      130     2
Comp.3      240     2
Comp.1      250     3
  .          .      .
  .          .      .
  .          .      .

I need to find the top 10 merchant with sales every month over 12 months.

It is very easy if it is just one month.

SELECT TOP 10 
       Merchant, 
       Sales, 
       Month
FROM   tbl_Merchant
WHERE  Month = 1
ORDER BY Sales DESC

But I am stuck if I wan to find them over 12 months. I need to display 120 merchants, which are top 10 sales merchant of each month. Currently my solution is to union 12 tables from month 1 to 12 together but I don't think it is a good way.

Can any one kindly give me any suggestion?

Thank you very much.


回答1:


Valid for Sql Server 2005+

Try this for overall:

SELECT TOP 10 
       Merchant, 
       SUM(Sales) Sales
FROM   tbl_Merchant
WHERE  Month BETWEEN 1 and 12
GROUP BY Merchant
ORDER BY 2 DESC

OR

if you need details per month for top 10 PER MONTH

;WITH MonthsCTE(m) as
(
    SELECT 1 m
    UNION ALL 
    SELECT m+1
    FROM MonthsCTE
    WHERE m < 12
)
SELECT m [Month], t.*
FROM MonthsCTE
CROSS APPLY 
(
    SELECT TOP 10
       Merchant, 
       SUM(Sales) Sales
    FROM   tbl_Merchant
    WHERE  Month = MonthsCTE.m
    GROUP BY Merchant
    ORDER BY 2 DESC
) t



回答2:


Try:

select * from
(SELECT Merchant, 
        Sales, 
        Month,
        row_number () over (partition by Month order by Sales desc) rn
 FROM   tbl_Merchant
 WHERE  Month between 1 and 12) v
where rn <= 10
order by Month, Sales desc

(Should work in SQLServer; not sure about Sybase.)




回答3:


select Top 10 
         SaleID, 
         sum(Salelines) As LINESUM
Into #TOP10sales
from salestable
group by SaleID
order by sum(Salelines) asc;

Then confirm the sales are top 10

select * 
from #TOP10sales;

Then.

Select field..
       field..
       field..
from #TOP10sales 
Inner Join salestable on #TOP10sales.ID = salestable.ID;



回答4:


Can try the following:

SELECT top 10 Merchant,
isnull(SUM(CASE WHEN Month(ta.dt) = '01' THEN Sales END),0) AS "Jan Sales",
isnull(SUM(CASE WHEN Month(ta.dt) = '02' THEN Sales END),0) AS "Feb Sales",
isnull(SUM(CASE WHEN Month(ta.dt) = '03' THEN Sales END),0) AS "Mar Sales",
isnull(SUM(CASE WHEN Month(ta.dt) = '04' THEN Sales END),0) AS "Apr Sales",
isnull(SUM(CASE WHEN Month(ta.dt) = '05' THEN Sales END),0) AS "May Sales",
isnull(SUM(CASE WHEN Month(ta.dt) = '06' THEN Sales END),0) AS "Jun Sales",
isnull(SUM(CASE WHEN Month(ta.dt) = '07' THEN Sales END),0) AS "Jul Sales",
isnull(SUM(CASE WHEN Month(ta.dt) = '08' THEN Sales END),0) AS "Aug Sales",
isnull(SUM(CASE WHEN Month(ta.dt) = '09' THEN Sales END),0) AS "Sep Sales",
isnull(SUM(CASE WHEN Month(ta.dt) = '10' THEN Sales END),0) AS "Oct Sales",
isnull(SUM(CASE WHEN Month(ta.dt) = '11' THEN Sales END),0) AS "Nov Sales",
isnull(SUM(CASE WHEN Month(ta.dt) = '12' THEN Sales END),0) AS "Dec Sales"

FROM tbl_Merchant ORDER BY Sales DESC

Top 10 sales of every month will be displayed as different columns by month.

Hope it helps u.




回答5:


Details per month for top 10 PER MONTH can be derived using this query too

select merchant,SUM(sales)Total_sales,month as Monthofyear
from tbl_Merchant
group by month,merchant,sales
order by month,sales desc




回答6:


select top 10 Merchant, sum(Sales) from tbl_Merchant group by Merchant order by sum(Sales) desc

I'm more postgresql guy, but i think it could work without (or with little) modification in mssql



来源:https://stackoverflow.com/questions/8801085/sql-top-10-sales-every-month

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!