使用窗口函数查询
create table hr
(
candidate_id INTEGER PRIMARY KEY, -- candidate unique identifier
candidate_gname VARCHAR(30) NOT NULL, -- candidate's given name
candidate_fname VARCHAR(30) NOT NULL, -- candidate's family name
position VARCHAR(30) NOT NULL, -- job position the candidate applied for
hired BOOLEAN NOT NULL, -- final hiring decision (yes/no)
zipcode CHAR(5) NOT NULL, -- hiring office zipcode
population CHAR(2) NOT NULL -- candidate's membership to the
-- "general" population (GP) or to the
-- "protected" population (PP)
);
EX1) “受保护”人口的成员资格是否会影响被雇用的随机候选人的机会?编写一个SQL查询,以计算相对于两个人群的正面和负雇用决策的相对频率(“ PP”和“ GP”)。
以下是我使用窗口函数编写的查询, 但是FREQ应该总结到人口最多1个WRT,但考虑到整个,我的疑问总结了1个。 我做什么变化? 预期结果:
FREQ | 雇用 | 人口 |
---|---|---|
0.49 | true | GP |
0.51 | 错误的 | GP |
0.52 | true | PP |
0.48 | false | PP |
我写过的
Select round((avg(freq1)/5000),2) as freq,hired,population
From (Select population,hired,count(hired)
over (partition by population, hired) as freq1
From hr
) as hr1
Group by population,hired
Order by population, hired desc;
查询:查询的结果:
freq | hired | population
------+-------+------------
0.15 | t | GP
0.45 | f | GP
0.06 | t | PP
0.34 | f | PP
(4 rows)
create table hr
(
candidate_id INTEGER PRIMARY KEY, -- candidate unique identifier
candidate_gname VARCHAR(30) NOT NULL, -- candidate's given name
candidate_fname VARCHAR(30) NOT NULL, -- candidate's family name
position VARCHAR(30) NOT NULL, -- job position the candidate applied for
hired BOOLEAN NOT NULL, -- final hiring decision (yes/no)
zipcode CHAR(5) NOT NULL, -- hiring office zipcode
population CHAR(2) NOT NULL -- candidate's membership to the
-- "general" population (GP) or to the
-- "protected" population (PP)
);
ex1)
Does membership to the “protected” population affect the chances of a random candidate of being hired? Write a SQL query to compute the relative frequency of positive and negative hiring decisions with respect to the two populations (“PP” and “GP”).
Below is the query I have written using window function,
But the freq should sum up to 1 wrt to population but my query is summing up to 1 considering the whole.
What changes chan I make ??
expected result:
freq | hired | population |
---|---|---|
0.49 | true | GP |
0.51 | false | GP |
0.52 | true | PP |
0.48 | false | PP |
Query I have written:
Select round((avg(freq1)/5000),2) as freq,hired,population
From (Select population,hired,count(hired)
over (partition by population, hired) as freq1
From hr
) as hr1
Group by population,hired
Order by population, hired desc;
result of the query:
freq | hired | population
------+-------+------------
0.15 | t | GP
0.45 | f | GP
0.06 | t | PP
0.34 | f | PP
(4 rows)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要了解人口(误导人口)人口,而不是除以5000
You need to know the population (bad name by the way) population rather than dividing by 5000