使用窗口函数查询

发布于 2025-01-21 06:03:25 字数 1552 浏览 4 评论 0原文

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.49trueGP
0.51错误的GP
0.52truePP
0.48falsePP

我写过的

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:

freqhiredpopulation
0.49trueGP
0.51falseGP
0.52truePP
0.48falsePP

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三岁铭 2025-01-28 06:03:25

您需要了解人口(误导人口)人口,而不是除以5000

drop table if exists t;
create table t
(population varchar(2),hired varchar(5));

insert into t values
('pp','true'),('pp','true'),('pp','false'),('pp','true'),('pp','false'),
('gp','true'),('gp','true'),('gp','false'),('gp','true'),('gp','false');

Select round((avg(freq1))/popcount,2) as freq,hired,population
From (Select population,hired,
      count(hired) over (partition by population, hired) as freq1,
      count(hired) over (partition by population) as popcount
        From t hr
    ) as hr1
Group by population,hired
Order by population, hired desc;

'0.60', 'true', 'gp'
'0.40', 'false', 'gp'
'0.60', 'true', 'pp'
'0.40', 'false', 'pp'

You need to know the population (bad name by the way) population rather than dividing by 5000

drop table if exists t;
create table t
(population varchar(2),hired varchar(5));

insert into t values
('pp','true'),('pp','true'),('pp','false'),('pp','true'),('pp','false'),
('gp','true'),('gp','true'),('gp','false'),('gp','true'),('gp','false');

Select round((avg(freq1))/popcount,2) as freq,hired,population
From (Select population,hired,
      count(hired) over (partition by population, hired) as freq1,
      count(hired) over (partition by population) as popcount
        From t hr
    ) as hr1
Group by population,hired
Order by population, hired desc;

'0.60', 'true', 'gp'
'0.40', 'false', 'gp'
'0.60', 'true', 'pp'
'0.40', 'false', 'pp'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文