postgresql 获取两个值之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17214956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the difference between two values
提问by ZeroGS
I got totally stuck on comparing two tables and getting the difference between them
我完全陷入了比较两个表并找出它们之间的差异
So here we go: I got table a with the following columns Name|Value|Date
所以我们开始了:我得到了包含以下列 Name|Value|Date 的表 a
and the second table b with the same columns
和第二个表 b 具有相同的列
What i wanna do now is get the difference between the values like
我现在想做的是得到像这样的值之间的差异
Table a
表一
Name|Value|Date
名称|值|日期
Test|3|2013-20-06
测试|3|2013-20-06
Table b
表b
Name|Value|Date
名称|值|日期
Test|9|2013-20-06
测试|9|2013-20-06
What i wann get is the difference between the 3 and 9 so i would recieve 6
我想要的是 3 和 9 之间的差异,所以我会收到 6
Any Idea how i'm able to get that from a query in my PostgreSQL-DB?
知道我如何从我的 PostgreSQL-DB 中的查询中得到它吗?
回答by Klas Lindb?ck
Join the tables and select the difference:
加入表格并选择差异:
select a.name, b.value - a.value, a.date
from a inner join b on a.name = b.name and a.date = b.date