从select语句的输出创建一个变量,并在另一个select语句POSTGRE SQL中使用该变量

时间:2018-02-15 16:41:32

标签: postgresql

说我有以下查询:

SELECT quantity
FROM TABLE A
WHERE account = xx
AND date = xxx-xx-xx

此查询返回一个值。假设我现在想在第二个查询中使用该值作为变量,如:

SELECT SUM(quantity) + variable as 'CURRENT POS'
FROM TABLE B
WHERE account = xx
AND date = xxx-xx-xx

我将如何做到这一点?

1 个答案:

答案 0 :(得分:0)

SELECT 
(SELECT SUM(quantity) FROM tb2 WHERE account = xx AND date = xxx-xx-xx) +
(SELECT SUM(quantity) FROM tb1 WHERE account = xx AND date = xxx-xx-xx) 

请注意,有一百万种方法可以解决它: - )