【问题标题】:How can i write hired the same month employees and order?我怎样才能写出同月雇佣的员工和订单?
【发布时间】:2020-09-26 11:54:33
【问题描述】:

Sql,它给出了在同一月(或同一年)雇用的员工数量,并按日期按顺序分组。

我尝试编写此代码,但没有找到相同的雇用和订单。

SELECT hire_date,COUNT(hire_date) 
FROM employees
GROUP BY hire_date;

【问题讨论】:

  • 简单。如果您想要按特定顺序排序的结果集,您必须使用 ORDER BY 子句。 GROUP BY 仅对数据进行分组,不对其进行排序/排序
  • 问题是如何对查询结果进行排序?使用order by

标签: sql oracle oracle-apex


【解决方案1】:

这就是我对问题的理解。

样本数据:

SQL> select ename, hiredate from emp order by hiredate;

ENAME      HIREDATE
---------- ----------
SMITH      17_12_1980
ALLEN      20_02_1981
WARD       22_02_1981
JONES      02_04_1981
BLAKE      01_05_1981
CLARK      09_06_1981
TURNER     08_09_1981
MARTIN     28_09_1981
KING       17_11_1981
JAMES      03_12_1981
FORD       03_12_1981
MILLER     23_01_1982
SCOTT      09_12_1982
ADAMS      12_01_1983

14 rows selected.

同月入职:

SQL> select to_char(hiredate, 'mm.yyyy') hire_month,
  2    count(*)
  3  from emp
  4  group by to_char(hiredate, 'mm.yyyy')
  5  order by 1;

HIRE_MO   COUNT(*)
------- ----------
01.1982          1
01.1983          1
02.1981          2
04.1981          1
05.1981          1
06.1981          1
09.1981          2
11.1981          1
12.1980          1
12.1981          2
12.1982          1

11 rows selected.

SQL>

同年入职:

SQL> select extract(year from hiredate) hire_year,
  2    count(*)
  3  from emp
  4  group by extract(year from hiredate)
  5  order by 1;

 HIRE_YEAR   COUNT(*)
---------- ----------
      1980          1
      1981         10
      1982          2
      1983          1

SQL>

【讨论】:

  • 感谢您的回答。为什么我们使用 order by 1 ?
  • 不客气。 ORDER BY 1 表示“按SELECT 列列表中的第一列排序。因此,不是键入ORDER BY extract (year from hiredate),而是较短的versionORDER BY 1
猜你喜欢
  • 2018-08-04
  • 1970-01-01
  • 2021-12-16
  • 2013-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-06
相关资源
最近更新 更多