我希望在同一个字段中获得两个条件的结果。数据库在表的同一字段中有两个不同的货币记录USD
和另一个TRY
。所以,我想得到一个结果,一个接一个地找到两个货币行,并在我们获得对应于美元的TRY结果的同时将TRY汇率换算为美元。
我想在此代码中添加我尝试访问的脚本:
SELECT e.Gk_2, f.ad, sum(g.sf_miktar) as Qty
from Stok00 e
LEFT OUTER JOIN Gecoust f on e.Gk_2 = f.kod
left outer join stok29t g on e.kod = g.kod
where e.kod like 'SAC SIL%' and f.Evrakno = 'STKGK2'
group by e.GK_2, f.ad
示例:
(Select sum(stock29.amount) from stock29 where stock29.rate = 'USD')
(Select sum(stock29.amount / 1.8) from stock29 where stock29.rate = 'TRY')
答案 0 :(得分:0)
如果我理解正确,那么您的查询应该看起来像这样(我只是在您的查询中添加一个字段,假设它有效):
SELECT e.Gk_2, f.ad, sum(g.sf_miktar) as Qty,
SUM(CASE WHEN stocks29.rate = 'USD' THEN stocks29.amount
WHEN stocks29.rate = 'TRY' THEN stocks29.amount / 1.8 END) As Amount
from Stok00 e
LEFT OUTER JOIN Gecoust f on e.Gk_2 = f.kod
left outer join stok29t g on e.kod = g.kod
where e.kod like 'SAC SIL%' and f.Evrakno = 'STKGK2'
group by e.GK_2, f.ad
我还假设stocks29.rate
和stocks29.amount
都不是NULL
。如果不是这种情况,那么它们应该适当地包含在ISNULL()
函数中。