我有一个表贷款,结构如下:
Ccy | interest1| interest2
GBP | 75 | 95
USD | 30 | 50我的任务如下:
我希望我的输出只有在interest1 + interest 2大于200时才有英镑,并且只有当interest 1+ interest 2大于50时输出才有USD。
输出应该如下所示,见英镑在输出中不存在,因为利息和只有170,低于200,因为USD在输出中大于50。
USD
80我使用了下面的查询。但是,它也显示了英镑。我不想让GBP被看到。它必须显示唯一的USD。因为,英镑不超过200英镑。
with a as (select ccy,(interest1 + interest2) as amount from my_table where
(case when (ccy = 'GBP' and (interest1+interest2) > 200) then 1
when (ccy = 'USD' and (interest1+interest2) > 50) then 1 end) = 1 )
Select * from a
pivot (sum(amount) for ccy in ('GBP' as GBP, 'USD' as USD))发布于 2018-11-03 21:39:09
Select Ccy, interest1, interest2
from table
where (Ccy = "GBP" and (interest1 + interest2) > 200) or (Ccy = "USD" and (interest1 + interest2) > 50)我不确定你是否希望两个数字加在一起更大,那么特定值或两个数字加在一起就必须更大。这就是解决方案,当一行中的两个数字加在一起更大时。
发布于 2018-11-03 21:44:51
嗯,你想做这个任务(我想)是为了从另一个地方(网络,服务……)给她打电话,然后我建议你从loan表中创建一个视图,然后在这个视图中你可以指定如何工作(如果interest1 + interest2 > 200,那么输出单位是英镑和所有你想要的)。我在这里向你展示了这个理论的一个链接:
https://www.w3schools.com/sql/sql_view.asp
然后,在知道这一点之后,你必须这样做:
-- SQL Code in 1 query: CREATE VIEW [Suitables_Prices] AS SELECT Ccy, interest1 + interest2 AS interest FROM LoanTable WHERE (Ccy = 'GBP' and (interest1 + interest2) > 200) or (Ccy = 'USD' and (interest1 + interest2) <= 200 and (interest1 + interest2) >= 50);
这将分别输出名称和感兴趣的总和。这样,你就是在用利息的价值旋转……
或者你也可以使用数据库中的过程来做同样的事情,但是我认为视图更简单更好。
https://stackoverflow.com/questions/53131767
复制相似问题