Sql查询替代方式

时间:2015-07-21 19:36:52

标签: sql oracle query-optimization

我想找到

从员工表中选择年龄和月份加入的员工数

我的查询是:

select to_char(JOINING_DATE,'YYYY') As "Year",
       to_char(JOINING_DATE,'MM') As "MOnth",
       Count(First_name) 
  from employee
 group by to_char(JOINING_DATE,'YYYY'),
          to_char(JOINING_DATE,'MM');

没有错误,成功运行。

有没有其他方法可以获得与此查询相同的结果?

3 个答案:

答案 0 :(得分:0)

如果您只按年份和月份分组,则可以执行以下操作:

SELECT to_char(JOINING_DATE, 'MM-YYYY'), Count(First_name)
FROM test_date
GROUP BY to_char(JOINING_DATE, 'MM-YYYY')

答案 1 :(得分:0)

您可能想要订购结果。我会将年份和月份放在同一列中:

Select to_char(JOINING_DATE, 'YYYY-MM') As YYYYMM", Count(*)
from employee
group by to_char(JOINING_DATE, 'YYYY-MM')
order by to_char(JOINING_DATE, 'YYYY-MM');

答案 2 :(得分:0)

您可以稍微简化一下您的查询并尝试:

SELECT YEAR(JOINING_DATE) As "Year", 
       MONTH(JOINING_DATE) As "Month",
       Count(First_name) 
FROM employee 
GROUP BY Year, Month;