Sql 查找百分位_cont

Sql 查找百分位_cont,sql,postgresql,Sql,Postgresql,希望我能表达清楚 我正在PostgreSQL 12中使用pgAdmin 4工具 我正在进行Anthony DeBarros的《实用SQL》一书中第(6)章末尾的练习,在运行查询时遇到了问题 有两个表格,每个州每个县的(总)人口,我想找出2000年到2010年的中值百分比变化 (p0010001是各表中各州和县的人口) 我想要的结果只是“第50百分位”的中位数 谢谢你能提供的任何帮助 2000年的表(2010年较大,但此查询不需要所有字段) 数据样本行(自2000年起): 2010年的样本行: N

希望我能表达清楚

我正在PostgreSQL 12中使用pgAdmin 4工具

我正在进行Anthony DeBarros的《实用SQL》一书中第(6)章末尾的练习,在运行查询时遇到了问题

有两个表格,每个州每个县的(总)人口,我想找出2000年到2010年的中值百分比变化

(p0010001是各表中各州和县的人口)

我想要的结果只是“第50百分位”的中位数

谢谢你能提供的任何帮助

2000年的表(2010年较大,但此查询不需要所有字段)

数据样本行(自2000年起):

2010年的样本行:

NAME,STUSAB,SUMLEV,REGION,DIVISION,STATE,COUNTY,AREALAND,AREAWATR,POP100,HU100,INTPTLAT,INTPTLON,P0010001,P0010002,P0010003,P0010004,P0010005,P0010006,P0010007,P0010008,P0010009,P0010010,P0010011,P0010012,P0010013,P0010014,P0010015,P0010016,P0010017,P0010018,P0010019,P0010020,P0010021,P0010022,P0010023,P0010024,P0010025,P0010026,P0010047,P0010063,P0010070,P0020001,P0020002,P0020003,P0020004,P0020005,P0020006,P0020007,P0020008,P0020009,P0020010,P0020011,P0020012,P0020028,P0020049,P0020065,P0020072,P0030001,P0030002,P0030003,P0030004,P0030005,P0030006,P0030007,P0030008,P0030009,P0030010,P0030026,P0030047,P0030063,P0030070,P0040001,P0040002,P0040003,P0040004,P0040005,P0040006,P0040007,P0040008,P0040009,P0040010,P0040011,P0040012,P0040028,P0040049,P0040065,P0040072,H0010001,H0010002,H0010003
    Autauga County,AL,050,3,6,01,001,1539582278,25775735,54571,22135,+32.5363818,-086.6444901,54571,53702,42855,9643,232,474,32,466,869,814,219,262,177,11,50,32,19,9,16,0,0,5,5,8,1,49,6,0,0,54571,1310,53261,52500,42154,9595,217,467,22,45,761,719,36,6,0,0,39958,39530,31910,6767,180,346,23,304,428,404,22,2,0,0,39958,828,39130,38746,31461,6738,169,341,15,22,384,363,19,2,0,0,22135,20221,1914
    Baldwin County,AL,050,3,6,01,003,4117521611,1133190229,182265,104061,+30.6592183,-087.7460666,182265,179542,156153,17105,1216,1348,89,3631,2723,2583,658,1035,336,35,311,63,34,7,38,12,0,15,11,14,14,128,11,1,0,182265,7992,174273,171976,152200,16966,1146,1340,79,245,2297,2205,87,5,0,0,140367,138905,122238,12272,923,1002,70,2400,1462,1398,60,3,1,0,140367,5186,135181,133937,119671,12193,876,994,62,141,1244,1198,46,0,0,0,104061,73180,30881
    Barbour County,AL,050,3,6,01,005,2291818968,50864716,27457,11829,+31.8706701,-085.4054562,27457,27199,13180,12875,114,107,29,894,258,254,76,49,7,2,34,54,2,4,7,0,1,2,1,5,10,3,0,0,1,27457,1387,26070,25861,12837,12820,60,107,24,13,209,206,2,0,0,1,21442,21275,10855,9647,86,80,24,583,167,163,3,0,0,1,21442,925,20517,20382,10624,9605,47,80,21,5,135,132,2,0,0,1,11829,9820,2009
    Bibb County,AL,050,3,6,01,007,1612480789,9289057,22915,8981,+33.0158929,-087.1271475,22915,22712,17381,5047,64,22,13,185,203,195,50,77,16,3,16,9,13,5,4,0,0,2,0,0,0,8,0,0,0,22915,406,22509,22328,17191,5024,64,22,7,20,181,175,6,0,0,0,17714,17584,13403,3975,47,14,11,134,130,128,2,0,0,0,17714,310,17404,17284,13247,3963,47,14,5,8,120,119,1,0,0,0,8981,7953,1028
    Blount County,AL,050,3,6,01,009,1669961855,15157440,57322,23887,+33.9774479,-086.5672464,57322,56638,53068,761,307,117,38,2347,684,662,112,330,74,8,102,12,2,0,6,3,0,2,4,7,0,21,1,0,0,57322,4626,52696,52129,50952,724,285,115,18,35,567,547,19,1,0,0,43216,42810,40515,549,227,91,23,1405,406,394,12,0,0,0,43216,2724,40492,40141,39285,524,212,89,14,17,351,341,10,0,0,0,23887,21578,2309
    Bullock County,AL,050,3,6,01,011,1613056905,6056528,10914,4493,+32.1017589,-085.7172613,10914,10828,2507,7666,23,20,43,569,86,82,12,28,3,0,3,8,5,2,9,0,0,3,0,0,9,4,0,0,0,10914,777,10137,10078,2392,7637,20,20,4,5,59,56,3,0,0,0,8484,8432,2164,5838,21,17,31,361,52,49,3,0,0,0,8484,485,7999,7963,2102,5821,18,17,3,2,36,33,3,0,0,0,4493,3745,748
    Butler County,AL,050,3,6,01,013,2011976894,2726814,20947,9964,+31.7516670,-086.6819689,20947,20786,11399,9095,60,177,7,48,161,153,58,41,17,1,13,10,4,3,3,0,0,3,0,0,0,8,0,0,0,20947,191,20756,20614,11324,9047,59,174,7,3,142,139,3,0,0,0,15891,15811,9109,6504,44,119,3,32,80,76,4,0,0,0,15891,130,15761,15694,9062,6468,43,117,3,1,67,65,2,0,0,0,9964,8491,1473
    Calhoun County,AL,050,3,6,01,015,1569189995,16624267,118572,53289,+33.7717061,-085.8225133,118572,116597,88840,24382,540,845,96,1894,1975,1848,676,520,223,49,143,67,48,20,46,8,2,4,17,17,8,109,17,0,1,118572,3893,114679,112975,87285,24177,480,830,94,109,1704,1602,93,8,0,1,91446,90441,70313,17746,451,665,70,1196,1005,939,62,3,0,1,91446,2473,88973,88088,69294,17621,409,653,69,42,885,826,56,2,0,1,53289,47331,5958
(注:2010表的列数超过2000列)

如何舍入此查询

编辑:此查询能够将百分位结果转换为小数点后1位

SELECT CAST(percentile_cont(.5)
    WITHIN GROUP (ORDER BY
    (CAST(c2010.p0010001 AS numeric(8,1)) - c2000.p0010001)
                  / c2000.p0010001 * 100) as numeric(4,1)) AS "50th Percentile" 
FROM us_counties_2010 as c2010 INNER JOIN us_counties_2000 as c2000
    on c2010.state_fips = c2000.state_fips
    and c2010.county_fips = c2000.county_fips;

请以表格文本的形式提供示例数据和预期结果,以便其他人能够更好地提供帮助。@GMB我添加了CREATE语句和示例数据。@LucStorms感谢您的帮助。我确实添加了on子句,但再次出现错误。我将在这里用on子句更新我的语句。错误状态“pct\U更改”列不存在“pct\U更改”是一个计算字段,因此不能按顺序使用该计数。但是应该允许计算:
。。。(订购人((c2010.p0010001-c2000.p0010001)/c2000.p0010001))
@LucStorms谢谢您的帮助。根据您的评论-我能够得到
50%的分数。我想知道你能否建议如何将结果四舍五入到小数点后1位?修改后的问题在我的帖子底部。再次感谢你提供的帮助。
NAME,STUSAB,SUMLEV,REGION,DIVISION,STATE,COUNTY,AREALAND,AREAWATR,POP100,HU100,INTPTLAT,INTPTLON,P0010001,P0010002,P0010003,P0010004,P0010005,P0010006,P0010007,P0010008,P0010009,P0010010,P0010011,P0010012,P0010013,P0010014,P0010015,P0010016,P0010017,P0010018,P0010019,P0010020,P0010021,P0010022,P0010023,P0010024,P0010025,P0010026,P0010047,P0010063,P0010070,P0020001,P0020002,P0020003,P0020004,P0020005,P0020006,P0020007,P0020008,P0020009,P0020010,P0020011,P0020012,P0020028,P0020049,P0020065,P0020072,P0030001,P0030002,P0030003,P0030004,P0030005,P0030006,P0030007,P0030008,P0030009,P0030010,P0030026,P0030047,P0030063,P0030070,P0040001,P0040002,P0040003,P0040004,P0040005,P0040006,P0040007,P0040008,P0040009,P0040010,P0040011,P0040012,P0040028,P0040049,P0040065,P0040072,H0010001,H0010002,H0010003
    Autauga County,AL,050,3,6,01,001,1539582278,25775735,54571,22135,+32.5363818,-086.6444901,54571,53702,42855,9643,232,474,32,466,869,814,219,262,177,11,50,32,19,9,16,0,0,5,5,8,1,49,6,0,0,54571,1310,53261,52500,42154,9595,217,467,22,45,761,719,36,6,0,0,39958,39530,31910,6767,180,346,23,304,428,404,22,2,0,0,39958,828,39130,38746,31461,6738,169,341,15,22,384,363,19,2,0,0,22135,20221,1914
    Baldwin County,AL,050,3,6,01,003,4117521611,1133190229,182265,104061,+30.6592183,-087.7460666,182265,179542,156153,17105,1216,1348,89,3631,2723,2583,658,1035,336,35,311,63,34,7,38,12,0,15,11,14,14,128,11,1,0,182265,7992,174273,171976,152200,16966,1146,1340,79,245,2297,2205,87,5,0,0,140367,138905,122238,12272,923,1002,70,2400,1462,1398,60,3,1,0,140367,5186,135181,133937,119671,12193,876,994,62,141,1244,1198,46,0,0,0,104061,73180,30881
    Barbour County,AL,050,3,6,01,005,2291818968,50864716,27457,11829,+31.8706701,-085.4054562,27457,27199,13180,12875,114,107,29,894,258,254,76,49,7,2,34,54,2,4,7,0,1,2,1,5,10,3,0,0,1,27457,1387,26070,25861,12837,12820,60,107,24,13,209,206,2,0,0,1,21442,21275,10855,9647,86,80,24,583,167,163,3,0,0,1,21442,925,20517,20382,10624,9605,47,80,21,5,135,132,2,0,0,1,11829,9820,2009
    Bibb County,AL,050,3,6,01,007,1612480789,9289057,22915,8981,+33.0158929,-087.1271475,22915,22712,17381,5047,64,22,13,185,203,195,50,77,16,3,16,9,13,5,4,0,0,2,0,0,0,8,0,0,0,22915,406,22509,22328,17191,5024,64,22,7,20,181,175,6,0,0,0,17714,17584,13403,3975,47,14,11,134,130,128,2,0,0,0,17714,310,17404,17284,13247,3963,47,14,5,8,120,119,1,0,0,0,8981,7953,1028
    Blount County,AL,050,3,6,01,009,1669961855,15157440,57322,23887,+33.9774479,-086.5672464,57322,56638,53068,761,307,117,38,2347,684,662,112,330,74,8,102,12,2,0,6,3,0,2,4,7,0,21,1,0,0,57322,4626,52696,52129,50952,724,285,115,18,35,567,547,19,1,0,0,43216,42810,40515,549,227,91,23,1405,406,394,12,0,0,0,43216,2724,40492,40141,39285,524,212,89,14,17,351,341,10,0,0,0,23887,21578,2309
    Bullock County,AL,050,3,6,01,011,1613056905,6056528,10914,4493,+32.1017589,-085.7172613,10914,10828,2507,7666,23,20,43,569,86,82,12,28,3,0,3,8,5,2,9,0,0,3,0,0,9,4,0,0,0,10914,777,10137,10078,2392,7637,20,20,4,5,59,56,3,0,0,0,8484,8432,2164,5838,21,17,31,361,52,49,3,0,0,0,8484,485,7999,7963,2102,5821,18,17,3,2,36,33,3,0,0,0,4493,3745,748
    Butler County,AL,050,3,6,01,013,2011976894,2726814,20947,9964,+31.7516670,-086.6819689,20947,20786,11399,9095,60,177,7,48,161,153,58,41,17,1,13,10,4,3,3,0,0,3,0,0,0,8,0,0,0,20947,191,20756,20614,11324,9047,59,174,7,3,142,139,3,0,0,0,15891,15811,9109,6504,44,119,3,32,80,76,4,0,0,0,15891,130,15761,15694,9062,6468,43,117,3,1,67,65,2,0,0,0,9964,8491,1473
    Calhoun County,AL,050,3,6,01,015,1569189995,16624267,118572,53289,+33.7717061,-085.8225133,118572,116597,88840,24382,540,845,96,1894,1975,1848,676,520,223,49,143,67,48,20,46,8,2,4,17,17,8,109,17,0,1,118572,3893,114679,112975,87285,24177,480,830,94,109,1704,1602,93,8,0,1,91446,90441,70313,17746,451,665,70,1196,1005,939,62,3,0,1,91446,2473,88973,88088,69294,17621,409,653,69,42,885,826,56,2,0,1,53289,47331,5958
CREATE TABLE us_counties_2010 (
    geo_name varchar(90),                    -- Name of the geography
    state_us_abbreviation varchar(2),        -- State/U.S. abbreviation
    summary_level varchar(3),                -- Summary Level
    region smallint,                         -- Region
    division smallint,                       -- Division
    state_fips varchar(2),                   -- State FIPS code
    county_fips varchar(3),                  -- County code
    area_land bigint,                        -- Area (Land) in square meters
    area_water bigint,                        -- Area (Water) in square meters
    population_count_100_percent integer,    -- Population count (100%)
    housing_unit_count_100_percent integer,  -- Housing Unit count (100%)
    internal_point_lat numeric(10,7),        -- Internal point (latitude)
    internal_point_lon numeric(10,7),        -- Internal point (longitude)

    -- This section is referred to as P1. Race:
    p0010001 integer,   -- Total population
    p0010002 integer,   -- Population of one race:
    p0010003 integer,       -- White Alone
    p0010004 integer,       -- Black or African American alone
    p0010005 integer,       -- American Indian and Alaska Native alone
    p0010006 integer,       -- Asian alone
    p0010007 integer,       -- Native Hawaiian and Other Pacific Islander alone
    p0010008 integer,       -- Some Other Race alone
    p0010009 integer,   -- Population of two or more races
    p0010010 integer,   -- Population of two races:
    p0010011 integer,       -- White; Black or African American
    p0010012 integer,       -- White; American Indian and Alaska Native
    p0010013 integer,       -- White; Asian
    p0010014 integer,       -- White; Native Hawaiian and Other Pacific Islander
    p0010015 integer,       -- White; Some Other Race
    p0010016 integer,       -- Black or African American; American Indian and Alaska Native
    p0010017 integer,       -- Black or African American; Asian
    p0010018 integer,       -- Black or African American; Native Hawaiian and Other Pacific Islander
    p0010019 integer,       -- Black or African American; Some Other Race
    p0010020 integer,       -- American Indian and Alaska Native; Asian
    p0010021 integer,       -- American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander
    p0010022 integer,       -- American Indian and Alaska Native; Some Other Race
    p0010023 integer,       -- Asian; Native Hawaiian and Other Pacific Islander
    p0010024 integer,       -- Asian; Some Other Race
    p0010025 integer,       -- Native Hawaiian and Other Pacific Islander; Some Other Race
    p0010026 integer,   -- Population of three races
    p0010047 integer,   -- Population of four races
    p0010063 integer,   -- Population of five races
    p0010070 integer,   -- Population of six races

    -- This section is referred to as P2. HISPANIC OR LATINO, AND NOT HISPANIC OR LATINO BY RACE
    p0020001 integer,   -- Total
    p0020002 integer,   -- Hispanic or Latino
    p0020003 integer,   -- Not Hispanic or Latino:
    p0020004 integer,   -- Population of one race:
    p0020005 integer,       -- White Alone
    p0020006 integer,       -- Black or African American alone
    p0020007 integer,       -- American Indian and Alaska Native alone
    p0020008 integer,       -- Asian alone
    p0020009 integer,       -- Native Hawaiian and Other Pacific Islander alone
    p0020010 integer,       -- Some Other Race alone
    p0020011 integer,   -- Two or More Races
    p0020012 integer,   -- Population of two races
    p0020028 integer,   -- Population of three races
    p0020049 integer,   -- Population of four races
    p0020065 integer,   -- Population of five races
    p0020072 integer,   -- Population of six races

    -- This section is referred to as P3. RACE FOR THE POPULATION 18 YEARS AND OVER
    p0030001 integer,   -- Total
    p0030002 integer,   -- Population of one race:
    p0030003 integer,       -- White alone
    p0030004 integer,       -- Black or African American alone
    p0030005 integer,       -- American Indian and Alaska Native alone
    p0030006 integer,       -- Asian alone
    p0030007 integer,       -- Native Hawaiian and Other Pacific Islander alone
    p0030008 integer,       -- Some Other Race alone
    p0030009 integer,   -- Two or More Races
    p0030010 integer,   -- Population of two races
    p0030026 integer,   -- Population of three races
    p0030047 integer,   -- Population of four races
    p0030063 integer,   -- Population of five races
    p0030070 integer,   -- Population of six races

    -- This section is referred to as P4. HISPANIC OR LATINO, AND NOT HISPANIC OR LATINO BY RACE
    -- FOR THE POPULATION 18 YEARS AND OVER
    p0040001 integer,   -- Total
    p0040002 integer,   -- Hispanic or Latino
    p0040003 integer,   -- Not Hispanic or Latino:
    p0040004 integer,   -- Population of one race:
    p0040005 integer,   -- White alone
    p0040006 integer,   -- Black or African American alone
    p0040007 integer,   -- American Indian and Alaska Native alone
    p0040008 integer,   -- Asian alone
    p0040009 integer,   -- Native Hawaiian and Other Pacific Islander alone
    p0040010 integer,   -- Some Other Race alone
    p0040011 integer,   -- Two or More Races
    p0040012 integer,   -- Population of two races
    p0040028 integer,   -- Population of three races
    p0040049 integer,   -- Population of four races
    p0040065 integer,   -- Population of five races
    p0040072 integer,   -- Population of six races

    -- This section is referred to as H1. OCCUPANCY STATUS
    h0010001 integer,   -- Total housing units
    h0010002 integer,   -- Occupied
    h0010003 integer    -- Vacant
);
SELECT CAST(percentile_cont(.5)
    WITHIN GROUP (ORDER BY
    (CAST(c2010.p0010001 AS numeric(8,1)) - c2000.p0010001)
                  / c2000.p0010001 * 100) as numeric(4,1)) AS "50th Percentile" 
FROM us_counties_2010 as c2010 INNER JOIN us_counties_2000 as c2000
    on c2010.state_fips = c2000.state_fips
    and c2010.county_fips = c2000.county_fips;