我正在使用PostgreSQL和Node.js.
我有2个表(cases
和countries
):
db=# SELECT * FROM cases;
id | disease | country | year | value | rate
------+------------+----------------+------+------------+----------
1 | Polio | Albania | 2016 | 15.00 |
2 | Polio | Austria | 2016 | 10.00 |
3 | Polio | Belgium | 2016 | 55.00 |
4 | Polio | Albania | 2015 | 120.00 |
5 | Polio | Austria | 2015 | 48.00 |
6 | Polio | Belgium | 2015 | 85.00 |
7 | Polio | Albania | 2014 | 0.00 |
8 | Polio | Austria | 2014 | 1.00 |
9 | Polio | Belgium | 2014 | 3.00 |
10 | Tet | Albania | 2016 | 6.00 |
11 | Tet | Austria | 2016 | 7.00 |
12 | Tet | Belgium | 2016 | 10.00 |
13 | Tet | Albania | 2015 | 0.00 |
14 | Tet | Austria | 2015 | 78.00 |
15 | Tet | Belgium | 2015 | 900.00 |
16 | Tet | Albania | 2014 | 10.00 |
17 | Tet | Austria | 2014 | 12.00 |
18 | Tet | Belgium | 2014 | 34.00 |
db=# SELECT * FROM countries;
id | country | year | population | capital | area
------+----------------+------+-------------+---------+------
1 | Albania | 2018 | 2934363.00 | |
2 | Albania | 2017 | 2930187.00 | |
3 | Albania | 2016 | 2926348.00 | |
4 | Albania | 2015 | 2923352.00 | |
5 | Albania | 2014 | 2920775.00 | |
6 | Austria | 2018 | 8751820.00 | |
7 | Austria | 2017 | 8735453.00 | |
8 | Austria | 2016 | 8712137.00 | |
9 | Austria | 2015 | 8678657.00 | |
10 | Austria | 2014 | 8633220.00 | |
11 | Belgium | 2018 | 11498519.00 | |
12 | Belgium | 2017 | 11429336.00 | |
13 | Belgium | 2016 | 11358379.00 | |
14 | Belgium | 2015 | 11287940.00 | |
15 | Belgium | 2014 | 11219161.00 | |
我想通过计算rate
相对于cases
的费率填写cases
表的population
列。
所以这是我应该做的计算:
Example of Albania/Polio/2016: (value/population)*100*1000 = (15.00/2926348.00)*100*1000 = 0.51
Example of Belgium/Tet/2015: (value/population)*100*1000 = (900.00/11287940.00)*100*1000 = 7.97
请注意,cases
中的年份从2014年到2016年,而countries
表中的年份从2014年到2018年。
我该怎么做?
如果我使用pgAdmin 4运行使用 Igor Romanchenko 建议的查询,则可以正常运行。 但是,如果我使用此方法运行查询,则会出现错误。
功能:
methods.fillRateColumnCases = async function() {
var queryFillRate = {
text: 'UPDATE cases' +
'SET rate = (cases.value / c.population) * 100 * 1000' +
'FROM countries c' +
'WHERE c.country = cases.country AND c.year = cases.year;'
};
return await client.query(queryFillRate)
.then(function(res) {
console.log('Table \'cases\' filled with \'rate\' values');
})
.catch(function(err) {
console.log('Error filling \'rate\' column of table \'cases\'', err.stack);
});
}
错误:
Error filling 'rate' column of table 'cases' error: errore di sintassi a o presso "="
at Connection.parseE (C:\...\node_modules\pg\lib\con
nection.js:545:11)
at Connection.parseMessage (C:\...\node_modules\pg\l
ib\connection.js:370:19)
at Socket.<anonymous> (C:\...\node_modules\pg\lib\co
nnection.js:113:22)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:594:20)
答案 0 :(得分:1)
在Postgres中,您可以将其标记为update
:
update cases
set rate = (cases.value / c.population) * 100 * 1000
from countries c
where c.country = cases.country and c.year = cases.year;