开发者

SELECT from SQL

开发者 https://www.devze.com 2022-12-11 22:11 出处:网络
I have a tab开发者_如何学Gole which contains the names of a country, the population of the country and the countries GDP, how would I display the names of the country and their per capita GDPSELECT na

I have a tab开发者_如何学Gole which contains the names of a country, the population of the country and the countries GDP, how would I display the names of the country and their per capita GDP


SELECT name, gdp/NullIf(population,0) AS PerCapitaGDP FROM "MyCountryTable"


SQL allows calculations to be performed inline, like:

SELECT Name, GDP / Population AS [Per Capita GDP] FROM YourTable


To deal with the 0 population division by zero, I'd suggest using a CASE statement

SELECT
    Name,
    CASE IsNull(population,0)
        WHEN 0
            THEN 0
        ELSE
            THEN gdp/population
    END AS 'PerCapitaGDP'
FROM Countries

This will work on MS SQL Server, you may need to look up syntax for which DBMS you are using (for example, it looks like MySQL uses END CASE instead of just END)

0

精彩评论

暂无评论...
验证码 换一张
取 消