sql - 使用 SQL 将前两行计算为第三行
问题描述
子查询给出以下输出(只有两行):
id | cp | sp |
----------------
1 | 200 | 300 |
----------------
2 | 400 | 350 |
从上表中,我怎样才能得到以下输出:
即第一表和第三行的联合=(row1/row2)*100
id | cp | sp |
----------------
1 | 200 | 300 |
----------------
2 | 400 | 350 |
----------------
3 | 50 |85.71|
解决方案
在 Redshift 中,我认为您需要union all
:
select id, cp, sp
from t
union all
select max(id) + 1,
max(case when id = 1 then cp end) * 100.0 / max(case when id = 2 then cp end),
max(case when id = 1 then sp end) * 100.0 / max(case when id = 2 then sp end)
from t
推荐阅读
- angular - defer() 不会对页面刷新产生影响
- powershell - PowerShell 比较输出
- python - 从 oauth2client.service_account 导入 ServiceAccountCredentials 时语法无效
- mysql - 在大型更新时从 mysql 触发器向 gearman 发送作业
- restructuredtext - 在 reStructuredText 中使用标签
- android - 在华为 P30 上使用 Intent 打开 App Launch
- r - 如何在数据文件中的多个 x 和 y 位置之间画线
- android - 如何在 Android / iOS 移动 SDK 产品中捕获日志和崩溃报告?
- java - 如何更改 jar 文件的 Java 类
- arrays - 比较两个数组并找出差异