本文介绍了如何从ration_card表计算成人和儿童的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在ration_card中计算成人和小孩
ration_card(id,ration_card_number,姓名,年龄)
I want to count adult and child in ration_card
ration_card (id,ration_card_number,name,age)
insert into ration_card values('1','1001','ABC','50')
insert into ration_card values('2','1001','DEF','20')
insert into ration_card values('3','1001','GHI','5')
insert into ration_card values('4','1001','JK','10')
insert into ration_card values(5,1002,'ABC',50)
insert into ration_card values(6,1002,'DEF',20)
insert into ration_card values(7,1002,'GHI',5)
insert into ration_card values(8,1002,'JK',10)
我的尝试:
What I have tried:
select count(name) as Adult from ration_card group by age having age>17
推荐答案
select count(*) as Adult from ration_card where age> 17
select count(*) as Child from ration_card where age< 17
SELECT Class, count(name)
FROM (
SELECT CASE WHEN age > 17 THEN 'Adults' ELSE 'Children' END AS Class, name
FROM ration_card) a
GROUP BY Class
SELECT
Sum(CASE WHEN Age > 17 THEN 1 ELSE 0 END) As Adults,
Sum(CASE WHEN Age <= 17 THEN 1 ELSE 0 END) As Children
FROM
ration_card
;
这篇关于如何从ration_card表计算成人和儿童的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!