SQL Query Simple Issue子选择

时间:2015-12-13 22:32:13

标签: sql db2

我必须显示来自该国家的一行中的输出,其中20-24范围内的男性人数最多。但是sex_ratio应该显示所有范围?

class Thing:
    """ Represents all possible creatures in the world.
    """


    def __init__(self, symbol, x, y):
        """
        """
        self.__symbol = symbol
        self.__position = (x,y)
        self.__age = 0


    def __repr__(self):
        return (self.__symbol)

    #print ('debug: i am', self, '. I´m at', self.__position)
    def performAction(self, world):
        self.__age += 1
        pass

class Plant(Thing):
    """ Represents all possible plants in the world.
    """

    def __init__(self, symbol, x, y):
        super().__init__(symbol, x, y)
        self.__seedCycle = 6

    def performAction(self, world):
        super().performAction(world)
        self.__seedCycle -=1
        if self.__seedCycle <= 0:
            randomdirection = [(0,1),(0,-1),(1,0),(-1,0)]
            random.shuffle(randomdirection)
            for i in randomdirection:
                x = world.getPos(self)[0] + i[0]
                y = world.getPos(self)[1] + i[1]
                if world.getObject(x, y) == '.':
                    world.spawn(self.__symbol, x, y)
                    # temporary fix: world.spawn(str(self), x, y)
                    break

它应该是这样的:

SELECT age,male ,female,sex_ratio,country                     
FROM census.population                                        
WHERE country<> 'Aggregated' and census.population.age='20-24'
ORDER BY male DESC FETCH FIRST 1 ROW ONLY;  

到目前为止我的输出:

 --*** select age,                                       ****  
    --***        total number of males,                     ****  
    --***        total number of females,                   ****  
    --***        sex_ratio,(of every age)                   ****  
    --***        country                                    ****  
    --***  where the country has the highest number of      ****  
    --***  males in the 20-24 age range                     ****  

结果应该是印度,因为它具有20-24岁范围内的最大男性人数,每个年龄的性别比(不仅仅是20-24岁)。 census.population.age ='总计'将提供每个年龄段的性别比例。我怎样才能获得总年龄的性别比例和20-24岁男性最多的国家?什么子选择适用于sex_ratio? 我的想法是这样,但它没有用。

AGE                    MALE                     FEMALE      SEX_RA  COUNTRY   
20-24                 58275712                 51794339      112.5  India       

我的数据库

SELECT age,male ,female,sex_ratio,country                     
FROM census.population                                        
WHERE country<> 'Aggregated' and census.population.age='20-24'
AND sex_ratio = (select sex_ratio from census.population
WHERE census.population.age = 'Total')
ORDER BY male DESC FETCH FIRST 1 ROW ONLY;  

2 个答案:

答案 0 :(得分:0)

好的,现在更清楚一点。是的,您需要一个子选择,在20-24岁年龄组中,您可以获得最多的男性,其中国家/地区不是聚合的。然后你得到这个国家(或者是一个平局的国家),最后你得到给定国家的摘要。

select t2.country, sum(t3.male) as totalmales, sum(t3.female) as totalfemales
from (select max(male) as maxmale from table where age='20-24' and country<>'Aggregate') t
inner join table t2 on t.maxmale=t2.male
inner join table t3 on t2.country=t3.country
where t2.age='20-24' and t2.country<>'Aggregate'
group by t2.country

如您所见,我在上述查询中未包含sex_ratio。原因是我不明白你想在那里看到什么。

答案 1 :(得分:0)

你可以尝试:

SELECT age, male, female, sex_ratio, country
FROM census.population
WHERE country = (SELECT country 
                 FROM census.population 
                 WHERE male = MAX(male)
                 AND sex_ratio = '20-24');

使用子查询选择具有最大男性数量且性别比例在20-24之间的国家/地区。您不必订购选择,因为返回只是一个条目。

或者您可以尝试:

SELECT age, male, female, sex_ratio, country
FROM census.population
GROUP BY country
HAVING MAX(male) 
AND sex_ratio = '20-24'
ORDER BY male DESC;

如果您真的想要订购所有结果,请使用HAVING子句,这将返回数据库中的所有国家/地区。