如何从 table 中 select 并将值除以 PostgreSQL 中另一个查询的结果?
How to select from a table and divide the values with the result of another query in PostgreSQL?
考虑一下我有这个 table:
create table currency(
currency char(3)
PRIMARY KEY,
rate decimal,
buy decimal,
sell decimal
);
我有这个数据:
insert into currency(currency,rate,buy,sell) values('EUR', 1, 1, 1);
insert into currency(currency,rate,buy,sell) values('USD', 0.8979, 0.901, 0.887);
insert into currency(currency,rate,buy,sell) values('GBP', 1.12404, 1.14405, 1.10543);
现在我想 select 基于美元汇率的汇率,然后我希望所有行的汇率、买入和卖出列除以美元汇率。即
我想要类似的东西
select currency, rate/<usd exchange rate>, buy/<usd exchange rate>, sell/<usd exchange rate> from currency;
可以select编辑为:
select rate from currency where currency='USD';
您可以在一次查询中从 table 中获取两次:
SELECT c.currency, c.rate / u.rate AS rate, c.buy / u.rate AS buy, c.sell / u.rate AS sell
FROM currency c, currency u
WHERE u.currency='USD';
将在完整的 table 和美元行之间进行交叉连接。
考虑一下我有这个 table:
create table currency(
currency char(3)
PRIMARY KEY,
rate decimal,
buy decimal,
sell decimal
);
我有这个数据:
insert into currency(currency,rate,buy,sell) values('EUR', 1, 1, 1);
insert into currency(currency,rate,buy,sell) values('USD', 0.8979, 0.901, 0.887);
insert into currency(currency,rate,buy,sell) values('GBP', 1.12404, 1.14405, 1.10543);
现在我想 select 基于美元汇率的汇率,然后我希望所有行的汇率、买入和卖出列除以美元汇率。即
我想要类似的东西
select currency, rate/<usd exchange rate>, buy/<usd exchange rate>, sell/<usd exchange rate> from currency;
可以select编辑为:
select rate from currency where currency='USD';
您可以在一次查询中从 table 中获取两次:
SELECT c.currency, c.rate / u.rate AS rate, c.buy / u.rate AS buy, c.sell / u.rate AS sell
FROM currency c, currency u
WHERE u.currency='USD';
将在完整的 table 和美元行之间进行交叉连接。