使用 AS 的 SQL 查询 [英] SQL Query using AS
本文介绍了使用 AS 的 SQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个处理两个表的查询:
I'm trying to create a query which deals with two tables:
表格:区域
Column Name Type Constraint
REGION_Cd Number Primary Key
REGION_Desc String
表格:统计数据
Column Name Type Constraint
AGE Number Primary Key
REGION Number Foreign Key to REGION (REGION_Cd)
POPULATION Number
我如何创建一个查询,以便找到两个年龄组(0-15、16-30)的人口并以以下格式显示:
How could I create a query such that it finds the population for two age groups (0-15, 16-30) and displayed in the following format:
Region_Desc AgeGroup Population
South 0‐15 11253
South 16‐30 235234
因为 AgeGroup 列实际上不是表中的列,所以我应该使用 AS 语句,对吗?
Since the AgeGroup column isnt actually a column in the tables, I should be using an AS statement, correct?
推荐答案
也许类似于...但它假设您想要年龄组/地区的人口总和.根据人口在数据库中的工作方式,这可能正确也可能不正确
Perhaps something like... but it assumes you want sum of population in age group/region. which may or maynot be correct depending on how population works in the database
Select Region_Desc, case when age between 0 and 15 then '0-15'
when age between 16 and 30 then '16-30'
else 'over 30' end as AgeGroup,
sum(Population) as Population
FROM Region INNER JOIN STATS on region_Cd = Region
GROUP BY Region_DESC, AgeGroup
这篇关于使用 AS 的 SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文