如何查询表中增加的列值

时间:2013-11-26 13:44:09

标签: ms-access

我遇到了这个问题,要找到下表中选民增长最快的地区,即在2001年,2005年和2010年找到地区增长率的最大平均百分比,或者比较每个地区在2001年,2005年和2010年的百分比。

Region          Electorate  Constituency                ElectionYear
--------------  ----------  --------------------------  ------------
South East          109044  Isle of Wight                       2010
South East          107737  Isle of Wight                       2005
South East          106305  Isle of Wight                       2001
North West           90124  Manchester Central                  2010
London               89601  East Ham                            2010
London               88624  Croydon North                       2010
East Midlands        88378  Northampton South                   2005
London               88236  Brentford and Isleworth             2005
East Midlands        88058  Daventry                            2005
South East           88006  Banbury                             2005
East Midlands        86537  Daventry                            2001
South West           86324  Devizes                             2005
London               85943  Ilford South                        2010
London               85851  Kingston and Surbiton               2010
East Midlands        85450  Sleaford and North Hykeham          2010
East Midlands        85271  Northampton South                   2001
West Midlands        85241  Stratford-on-Avon                   2001
London               85078  West Ham                            2010
South West           84821  Bristol West                        2001
West Midlands        84540  Stratford-on-Avon                   2005
South East           84379  Banbury                             2010
London               84049  Brentford and Isleworth             2001
South East           83935  Winchester                          2005
London               83904  Harrow East                         2005

我试着通过编写这段代码来看这个,但我认为我做的不对;我认为还有其他更好的方法:

SELECT DISTINCT Region, Electorate 
FROM DW_FastGrowingElectorateRegion 
WHERE Electorate = (SELECT MAX(Electorate) FROM DW_FastGrowingElectorateRegion) 
    OR Electorate = (SELECT MIN(Electorate) FROM DW_FastGrowingElectorateRegion ) 
GROUP BY Region, Electorate 
ORDER BY Electorate DESC;

结果如下:

Region           Electorate
South East       109044
Scotland         31909

任何帮助都会有所帮助,并且非常感谢,因为我花了好几天时间尝试对此进行排序。

谢谢。

1 个答案:

答案 0 :(得分:1)

您当前的查询仅选择原始数据中具有[Electorate]最高和最低值的行。您的查询中有一个GROUP BY子句,但您实际上并没有进行任何聚合。

首先在Access中创建一个名为[ElectorateSumsByRegionYear]的已保存查询,如下所示

SELECT 
    Region, 
    ElectionYear,
    Sum(Electorate) AS SumOfElectorate
FROM DW_FastGrowingElectorateRegion 
GROUP BY Region, ElectionYear

对于您提供的测试数据,它会返回

Region          ElectionYear  SumOfElectorate
--------------  ------------  ---------------
East Midlands           2001           171808
East Midlands           2005           176436
East Midlands           2010            85450
London                  2001            84049
London                  2005           172140
London                  2010           435097
North West              2010            90124
South East              2001           106305
South East              2005           279678
South East              2010           193423
South West              2001            84821
South West              2005            86324
West Midlands           2001            85241
West Midlands           2005            84540

现在我们可以在Access中创建一个名为[ElectorateSumsCurrentPrevious]的已保存查询,以返回结果以及上一期间的相应结果

SELECT 
    CurrYear.Region, 
    ElectCurrPrevYr.ElectionYear AS CurrYear, 
    CurrYear.SumOfElectorate AS CurrSum, 
    ElectCurrPrevYr.PrevYear, 
    PrevYear.SumOfElectorate AS PrevSum
FROM 
    (
        ElectorateSumsByRegionYear AS CurrYear 
        INNER JOIN 
        (
            SELECT t1.Region, t1.ElectionYear, Max(t2.ElectionYear) AS PrevYear
            FROM 
                ElectorateSumsByRegionYear AS t1
                INNER JOIN
                ElectorateSumsByRegionYear AS t2
                    ON t1.Region = t2.Region
                        AND t1.ElectionYear > t2.ElectionYear
            GROUP BY t1.Region, t1.ElectionYear
        ) AS ElectCurrPrevYr 
            ON (CurrYear.ElectionYear = ElectCurrPrevYr.ElectionYear) 
                AND (CurrYear.Region = ElectCurrPrevYr.Region)
    ) 
    INNER JOIN 
    ElectorateSumsByRegionYear AS PrevYear 
        ON (ElectCurrPrevYr.PrevYear = PrevYear.ElectionYear) 
            AND (ElectCurrPrevYr.Region = PrevYear.Region);

返回

Region          CurrYear  CurrSum  PrevYear  PrevSum
--------------  --------  -------  --------  -------
East Midlands       2005   176436      2001   171808
East Midlands       2010    85450      2005   176436
London              2005   172140      2001    84049
London              2010   435097      2005   172140
South East          2005   279678      2001   106305
South East          2010   193423      2005   279678
South West          2005    86324      2001    84821
West Midlands       2005    84540      2001    85241

我们可以使用该查询作为任何"增长的基础"我们想要使用的衡量标准,例如

SELECT 
    Region, 
    CurrYear, 
    CurrSum, 
    PrevYear, 
    PrevSum, 
    [CurrSum]-[PrevSum] AS Increase, 
    (CDbl([CurrSum])/CDbl([PrevSum]))^(1/([CurrYear]-[PrevYear]))-1 AS AvgAnnualGrowthRate
FROM ElectorateSumsCurrentPrevious;

返回

Region          CurrYear  CurrSum  PrevYear  PrevSum  Increase  AvgAnnualGrowthRate  
--------------  --------  -------  --------  -------  --------  ---------------------
East Midlands       2005   176436      2001   171808      4628   6.66728558280982E-03
East Midlands       2010    85450      2005   176436    -90986     -0.134982342500498
London              2005   172140      2001    84049     88091       0.19629231270215
London              2010   435097      2005   172140    262957       0.20376263338977
South East          2005   279678      2001   106305    173373      0.273579774134796
South East          2010   193423      2005   279678    -86255  -7.10978631581222E-02
South West          2005    86324      2001    84821      1503   4.40078147996426E-03
West Midlands       2005    84540      2001    85241      -701  -2.06230641894412E-03

编辑重新评论

如果您想要一个只返回最高和最低增长率的查询,请将上述查询保存为[ElectorateGrowthByRegionYear],然后使用类似

的内容
    SELECT TOP 1 * FROM ElectorateGrowthByRegionYear
    ORDER BY AvgAnnualGrowthRate DESC
UNION ALL
    SELECT TOP 1 * FROM ElectorateGrowthByRegionYear
    ORDER BY AvgAnnualGrowthRate ASC