显示销售月份以及按销售额降序排列的当月完成的销售数量。
我使用了提取函数从日期中提取月份,但它给出的是月份编号而不是月份的全名
select extract(month from sldate) as month, count(sid) as number_sale from sale
group by sldate
order by sid desc
这是桌子
销售表
SALEID SID SLDATE
1001 1 01-JAN-14
1002 5 02-JAN-14
1003 4 01-FEB-14
1004 1 01-MAR-14
1005 2 01-FEB-14
1006 1 01-JUN-15
预期结果
MONTH NUMBER_SALE
February 2
January 2
March 1
June 1
这回答了用 MySql 标记的原始问题。
您需要将其与年份结合起来,而不是仅提取月份,因为您不想混合不同年份同月的销售额,因此使用
year_month
,然后使用 monthname()
来获取当月的销售额名称:
select
monthname(concat(extract(year_month from sldate), '01')) as month,
count(sid) as number_sale
from sale
group by month
order by number_sale desc
查看演示。
结果:
| month | number_sale |
| -------- | ----------- |
| February | 2 |
| January | 2 |
| March | 1 |
| June | 1 |
select to_char(sldate,'Month') as Month,count(*) as number_sale from sale group by to_char(sldate,'Month') order by count(*) desc
这是更简单的解决方案
SELECT TO_CHAR(SLDATE, 'Month') Month, COUNT(SALEID) NUMBER_SALE FROM Sale GROUP BY TO_CHAR(SLDATE, 'Month') ORDER BY NUMBER_SALE DESC
select to_char(sldate,'Month') Month,
count(to_char(sldate,'Month')) NUMBER_SALE
from sale group by (to_char(sldate,'Month'))
order by NUMBER_SALE desc
输出
MONTH NUMBER_SALE
January 2
February 2
March 1
June 1
select to_char(sldate,'Month') as month,count(sid) as NUMBER_SALE from sale
group by to_char(sldate,'Month')
order by NUMBER_SALE desc
检查下面的查询
SELECT TO_CHAR(SLDATE,'Month')"MONTH", COUNT(SID) AS NUMBER_SALE
FROM SALE
GROUP BY TO_CHAR(SLDATE,'Month')
ORDER BY COUNT(SID) DESC