query display like aging [message #684059] |
Tue, 30 March 2021 08:11  |
 |
glmjoy
Messages: 184 Registered: September 2011 Location: KR
|
Senior Member |
|
|
CREATE TABLE ABC (EMP_NO VARCHAR2(5),AGE number(3))
insert into ABC (EMP_NO,AGE) values ('00001',15);
insert into ABC (EMP_NO,AGE) values ('00001',20);
insert into ABC (EMP_NO,AGE) values ('00001',22);
insert into ABC (EMP_NO,AGE) values ('00001',24);
insert into ABC (EMP_NO,AGE) values ('00001',28);
insert into ABC (EMP_NO,AGE) values ('00001',34);
insert into ABC (EMP_NO,AGE) values ('00001',38);
insert into ABC (EMP_NO,AGE) values ('00001',40);
I want query display as below like aging
10-20 21-30 31-40
2 3 3
Counting Employees between 10 to 20 as so on 30 to 40
[Updated on: Tue, 30 March 2021 08:14] Report message to a moderator
|
|
|
|
|
|
Re: query display like aging [message #684063 is a reply to message #684059] |
Tue, 30 March 2021 15:15   |
 |
Michel Cadot
Messages: 68417 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Having numbers per tens may be useful for an employer but other categories may also.
For instance, in France, it is forbidden to employ children less than 14, for age 14 and 15 you need the authorization of the parents and the factory inspector, for age 16 and 17 you only need the parents authorization. You can retire since 62 if you have subscribe sufficient quarters otherwise you have to wait 67. When you reach 70, your boss may ask you to retire (or "voluntarily" resign).
This leads to the following query with in the middle the tens and at the left and right ends the specials cases:
SQL> select count(case when age < 14 then age end) "<14",
2 count(case when age between 14 and 15 then age end) "14-15",
3 count(case when age between 16 and 17 then age end) "16-17",
4 count(case when age between 18 and 19 then age end) "18-19",
5 count(case when age between 10 and 19 then age end) "10-19",
6 count(case when age between 20 and 29 then age end) "20-29",
7 count(case when age between 30 and 39 then age end) "30-39",
8 count(case when age between 40 and 49 then age end) "40-49",
9 count(case when age between 50 and 59 then age end) "50-59",
10 count(case when age between 60 and 69 then age end) "60-69",
11 count(case when age between 60 and 61 then age end) "60-61",
12 count(case when age between 62 and 66 then age end) "62-66",
13 count(case when age between 67 and 69 then age end) "67-69",
14 count(case when age >= 70 then age end) ">=70"
15 from abc
16 /
<14 14-15 16-17 18-19 10-19 20-29 30-39 40-49 50-59 60-69 60-61 62-66 67-69 >=70
------ ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
0 1 0 0 1 4 2 1 0 0 0 0 0 0

|
|
|
|
|