0

我想在与列相同的字段上获得两个条件的结果。数据库有两个不同的货币记录,一个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')
4

1 回答 1

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()适当地包装在函数中。

于 2013-04-13T04:30:38.623 回答