我在SQLite中构建了一个关系数据库来存储世界各国和地区的冠状病毒数据。数据库模式如下:
国家(名称、Population)
)
主键加下划线。外键用星号(*)表示。
我编写了以下代码,以显示按国家分列的每百万人口死亡人数:
SELECT CountryName,
MAX(CumulativeDeaths) AS ConfirmedDeaths,
(ConfirmedDeaths*1000000/Population) AS DeathsPerMillion
FROM Country c
INNER JOIN CountryData d ON (c.Name=d.CountryName)
GROUP BY d.CountryName当我在SQLite中执行查询时,它返回了错误消息:“没有这样的列:已确认死亡”。为什么它会返回这样的错误信息?如何修正这个错误以获得我想要达到的目标?
发布于 2020-06-10 01:58:49
对,是这样。不能在定义列别名的同一个select中重用它(也不能在from或where中使用)。
计算简单。所以重复一遍:
SELECT CountryName,
MAX(CumulativeDeaths) AS ConfirmedDeaths,
(MAX(CumulativeDeaths)*1000000/Population) AS DeathsPerMillion
FROM Country c INNER JOIN
CountryData d
ON c.Name = d.CountryName
GROUP BY d.CountryName, Population;请注意,您也是指Population中的SELECT。它应该是GROUP BY的一部分。或者是聚合函数(如MAX()或SUM() )的参数。
https://stackoverflow.com/questions/62294640
复制相似问题