SQL年龄段和唯一编号

时间:2019-05-15 07:08:38

标签: mysql sql

我如何使用具有以下字段的临时表编写脚本:NAME,SURNAME,AGE,OS_NAME。从临时表的结果中,删除所有年龄小于15岁的客户端。结果,显示手机的操作系统以及拥有此操作系统的人数。

表名-“ AGE_GROUPS”

+-----------------------------------+
|NAME   | SURNAME  |AGE |OS_NAME    |
+-----------------------------------+
|Ivan   | Dulin    | 14 | Android8.1|
|-----------------------------------|
|Sasha  | Hlo      | 13 | WP        |
|-----------------------------------|
|Andrey | Zac      | 16 | IOS       |
|-----------------------------------|
|Carld  | Marks    | 17 |WP         |
|-----------------------------------|
|Dan    | Smiz     | 15 | IOS       |
|-----------------------------------|
|Ivan   | Meisa    | 12 | Android8.1|
|-----------------------------------|
|Pasha  | Las      | 4  | IOS       |
|-----------------------------------|
|Stas   | Istomin  | 25 | Android8.1|
|-----------------------------------|
|Chuan  | Zen      | 55 | WP        |
|-----------------------------------|
|Marina | Buzinovna|30  |IOS        |
+-----------------------------------+

我无法确定SELECT后,年龄> = 15的NAME,SURNAME,AGE,OS_NAME会发生什么  没有时间学习“分组依据” 我的老板严厉地责骂我:(

2 个答案:

答案 0 :(得分:3)

首先,您可以在..和分组依据

时使用大小写
select  case when  age between  1 and 12 then '1-12 children' 
             when  age between  13 and 18 then '13-18 - adolescents' 
             when  age between  19 and 23 then '19-23 - students' 
             when  age between  24 and 50 then '24-50 - adults'    
             else   '51+ - pensioners'   
        end   Group_by_age ,
        count(*)
from  my_table  
group by  Group_by_ag 

答案 1 :(得分:0)

该问题与原始问题有很大不同,但是如果您确实要“显示手机的操作系统以及拥有此操作系统的人数”,那么

drop table if exists t;
create table t
(NAME varchar(20), SURNAME varchar(20),AGE int,OS_NAME varchar(20));
insert into t values
('Ivan'  , 'Dulin'  ,14 , 'Android8.1'),
('Sasha' , 'Hlo'    ,13 , 'WP'),
('Andrey' , 'Zac'   ,16 , 'IOS'),
('Carld' , 'Marks'  ,17 , 'WP'),
('Dan' , 'Smiz'     ,15 , 'IOS'),
('Ivan' , 'Meisa'   ,12 , 'Android8.1'),
('Pasha' , 'Las'    ,4  , 'IOS'),
('Stas' , 'Istomin' ,25 , 'Android8.1'),
('Chuan' , 'Zen'    ,55 , 'WP'),
('Marina' , 'Buzinovna',  30 ,'IOS');

select os_name, count(*)
from t
where age > 15
group by os_name;

+------------+----------+
| os_name    | count(*) |
+------------+----------+
| Android8.1 |        1 |
| IOS        |        2 |
| WP         |        2 |
+------------+----------+
3 rows in set (0.00 sec)