按2列分组后,组之间有区别
我有下表:
id | date_of_birth | 性别 |
---|---|---|
1 | 01/01/1990 | 男性 |
2 | 01/01/1961 | 女性 |
3 | 01/01/1992 | 女性 |
4 | 01/01/2000 | 男性 |
5 | 01/01/11/1980 | 男性 |
6 | 01/01/01/1990 | 女性 |
作为输出,我希望每十年的男性和女性人数之间的差异:
十年_of_birth | emair_minus_male |
---|---|
1960 | 1 |
1980 | -1 |
1990 -190 | 1 |
2000 | -1, |
例如,在90年代的十年中,1个男性出生,2个女性和2个女性 - > 2-1 = 1
到目前为止,我得到了:
CREATE TABLE person (
id BIGSERIAL NOT NULL PRIMARY KEY,
date_of_birth DATE,
gender VARCHAR(50)
);
insert into person (id, date_of_birth, gender) values (1, '01/01/1990', 'male');
insert into person (id, date_of_birth, gender) values (2, '01/01/1961', 'female');
insert into person (id, date_of_birth, gender) values (3, '01/01/1992', 'female');
insert into person (id, date_of_birth, gender) values (4, '01/01/2000', 'male');
insert into person (id, date_of_birth, gender) values (5, '01/01/1980', 'male');
insert into person (id, date_of_birth, gender) values (6, '01/01/1990', 'female');
SELECT *
FROM crosstab($$
SELECT EXTRACT(DECADE
FROM date_of_birth) * 10 AS decade_of_birth,
gender,
COUNT(DISTINCT id) AS nbr_persons
FROM person
GROUP BY 1,2
$$, $$ SELECT DISTINCT gender FROM person; $$) AS ct( decade_of_birth int, "Male" int, "Female" int);
唯一剩下的就是获得2列之间的差异。我知道我可以通过在子查询中编写并简单地提取2列,但是我想知道串扰中是否有更优雅的方式来获得所需的输出。
I have the following table:
id | date_of_birth | gender |
---|---|---|
1 | 01/01/1990 | male |
2 | 01/01/1961 | female |
3 | 01/01/1992 | female |
4 | 01/01/2000 | male |
5 | 01/01/1980 | male |
6 | 01/01/1990 | female |
As output I want the difference between number of males and females per decade:
decade_of_birth | female_minus_male |
---|---|
1960 | 1 |
1980 | -1 |
1990 | 1 |
2000 | -1 |
For example, in the decade of the 90s, 1 male was born, and 2 females --> 2-1 = 1
So far I got this:
CREATE TABLE person (
id BIGSERIAL NOT NULL PRIMARY KEY,
date_of_birth DATE,
gender VARCHAR(50)
);
insert into person (id, date_of_birth, gender) values (1, '01/01/1990', 'male');
insert into person (id, date_of_birth, gender) values (2, '01/01/1961', 'female');
insert into person (id, date_of_birth, gender) values (3, '01/01/1992', 'female');
insert into person (id, date_of_birth, gender) values (4, '01/01/2000', 'male');
insert into person (id, date_of_birth, gender) values (5, '01/01/1980', 'male');
insert into person (id, date_of_birth, gender) values (6, '01/01/1990', 'female');
SELECT *
FROM crosstab($
SELECT EXTRACT(DECADE
FROM date_of_birth) * 10 AS decade_of_birth,
gender,
COUNT(DISTINCT id) AS nbr_persons
FROM person
GROUP BY 1,2
$, $ SELECT DISTINCT gender FROM person; $) AS ct( decade_of_birth int, "Male" int, "Female" int);
The only thing that remains is getting the difference between the 2 columns. I know I can it by writing in a subquery and simply extract the 2 columns, but I was wondering if there is a more elegant way within the crosstab to get the desired output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)