2015-03-02 92 views
1

我试图在SQL Server中使用CASE语句。我试图更新ProfitUSD列,其中从Profit列中提取的值乘以Rate列的转换率得到Profit(USD)(ProfitUSD列)。基本上,我只是试图通过乘以从Rate列中获取的各自转换率来将利润转换为美元。在SQL服务器中使用案例

任何帮助将非常感激:)

这里的问题是,当我运行此我得到以下错误:

消息512,级别16,状态1,行1
子查询返回了超过1个值。当子查询遵循=,!=,<,< =,>,> =或当子查询用作表达式时,这是不允许的。 该语句已终止

代码:

update dbo.[5 Mins] 
set ProfitUSD = 
    case 
    when Region='AEX 30 Netherlands' then Profit*(select rate from dbo.rates where Region='AEX 30 Netherlands') 
    when Region='ASX Australia' then Profit*(select Rate from dbo.rates where Region='ASX Australia') 
    when Region='Athens' then Profit*(select Rate from dbo.rates where Region='Athens') 
    when Region='Austria' then Profit*(select Rate from dbo.rates where Region='Austria') 
    when Region='Bahrain' then Profit*(select Rate from dbo.rates where Region='Bahrain') 
    when Region='Bovespa' then Profit*(select Rate from dbo.rates where Region='Bovespa') 
    when Region='Brussels' then Profit*(select Rate from dbo.rates where Region='Brussels') 
    when Region='Bucharest' then Profit*(select Rate from dbo.rates where Region='Bucharest') 
    when Region='Budapest' then Profit*(select Rate from dbo.rates where Region='Budapest') 
    when Region='Bulgaria' then Profit*(select Rate from dbo.rates where Region='Bulgaria') 
    when Region='CAC 40' then Profit*(select Rate from dbo.rates where Region='CAC 40') 
    when Region='CBOT' then Profit*(select Rate from dbo.rates where Region='CBOT') 
    when Region='CME Globex' then Profit*(select Rate from dbo.rates where Region='CME Globex') 
    when Region='Comex Metal' then Profit*(select Rate from dbo.rates where Region='Comex Metal') 
    when Region='Copanhagen' then Profit*(select Rate from dbo.rates where Region='Copanhagen') 
    when Region='DJ Euro Stoxx 50' then Profit*(select Rate from dbo.rates where Region='DJ Euro Stoxx 50') 
    when Region='Doha' then Profit*(select Rate from dbo.rates where Region='Doha') 
    when Region='Egypt' then Profit*(select Rate from dbo.rates where Region='Egypt') 
    when Region='FTSE' then Profit*(select Rate from dbo.rates where Region='FTSE') 
    when Region='FTSE Malaysia' then Profit*(select Rate from dbo.rates where Region='FTSE Malaysia') 
    when Region='Hang Seng' then Profit*(select Rate from dbo.rates where Region='Hang Seng') 
    when Region='Helsinki' then Profit*(select Rate from dbo.rates where Region='Helsinki') 
    when Region='ICE Nybot' then Profit*(select Rate from dbo.rates where Region='ICE Nybot') 
    when Region='ICEX Iceland' then Profit*(select Rate from dbo.rates where Region='ICEX Iceland') 
    when Region='Istanbul' then Profit*(select Rate from dbo.rates where Region='Istanbul') 
    when Region='Johannesberg' then Profit*(select Rate from dbo.rates where Region='Johannesberg') 
    when Region='Lima' then Profit*(select Rate from dbo.rates where Region='Lima') 
    when Region='Lisbon' then Profit*(select Rate from dbo.rates where Region='Lisbon') 
    when Region='Moroccan' then Profit*(select Rate from dbo.rates where Region='Moroccan') 
    when Region='Moscow' then Profit*(select Rate from dbo.rates where Region='Moscow') 
    when Region='New Zeland NZX' then Profit*(select Rate from dbo.rates where Region='New Zeland NZX') 
    when Region='Nigeria 30 Lagos' then Profit*(select Rate from dbo.rates where Region='Nigeria 30 Lagos') 
    when Region='Nse All' then Profit*(select Rate from dbo.rates where Region='Nse All') 
    when Region='Oman' then Profit*(select Rate from dbo.rates where Region='Oman') 
    when Region='Oslo' then Profit*(select Rate from dbo.rates where Region='Oslo') 
    when Region='Parague' then Profit*(select Rate from dbo.rates where Region='Parague') 
    when Region='Philippines' then Profit*(select Rate from dbo.rates where Region='Philippines') 
    when Region='Santiago' then Profit*(select Rate from dbo.rates where Region='Santiago') 
    when Region='Saudi' then Profit*(select Rate from dbo.rates where Region='Saudi') 
    when Region='Shanghai' then Profit*(select Rate from dbo.rates where Region='Shanghai') 
    when Region='Slovenia' then Profit*(select Rate from dbo.rates where Region='Slovenia') 
    when Region='Spain' then Profit*(select Rate from dbo.rates where Region='Spain') 
    when Region='STI Singapore' then Profit*(select Rate from dbo.rates where Region='STI Singapore') 
    when Region='Stockholm' then Profit*(select Rate from dbo.rates where Region='Stockholm') 
    when Region='Swiss' then Profit*(select Rate from dbo.rates where Region='Swiss') 
    when Region='Toronto' then Profit*(select Rate from dbo.rates where Region='Toronto') 
    when Region='TSE Tokyo' then Profit*(select Rate from dbo.rates where Region='TSE Tokyo') 
    when Region='Tunisia' then Profit*(select Rate from dbo.rates where Region='Tunisia') 
    when Region='Turquise Italy' then Profit*(select Rate from dbo.rates where Region='Turquise Italy') 
    when Region='Warsaw' then Profit*(select Rate from dbo.rates where Region='Warsaw') 
    when Region='NASDAQ 100' then Profit*(select Rate from dbo.rates where Region='NASDAQ 100') 
    when Region='Abu Dhabi' then Profit*(select Rate from dbo.rates where Region='Abu Dhabi') 
    when Region='Nordic' then Profit*(select Rate from dbo.rates where Region='Nordic') 
    when Region='NYSE All' then Profit*(select Rate from dbo.rates where Region='NYSE All') 
    when Region='Seoul' then Profit*(select Rate from dbo.rates where Region='Seoul') 
    when Region='Taiwan' then Profit*(select Rate from dbo.rates where Region='Taiwan') 
    when Region='Ukraine' then Profit*(select Rate from dbo.rates where Region='Ukraine') 
    else ProfitUSD 
    end 
+9

jeezus,这实际上是错误的方式来执行此'UPDATE'。首先,你会得到那个错误,因为其中一些'(选择Rate from dbo.rates where .....)'返回的不止是一行。现在,代替这个巨大的'CASE'表达式,您应该做一个'JOIN' – Lamak 2015-03-02 20:41:43

+5

您必须至少有一个场景,其中一个区域有多个费率。 (为什么'区域'不是唯一的?或者它是另一个唯一键的一部分,为什么这个子句没有更多的限制?)另外,呃。你有没有考虑使用连接而不是这个令人发指的'CASE' **表达式**? – 2015-03-02 20:41:43

回答

1

该错误表示您的一个子查询(select rate from dbo.rates where region =)正在返回多个值。您至少有一个该表上的区域名称有两个(或更多)记录。

您可以(应该)使用JOIN而不是巨大的CASE声明来执行此更新;添加新区域时会发生什么?

update A set a.profitusd= a.profit*r.rate from dbo.[5 min] A join dbo.rates R on a.region = r.region 

但是这并不能解决rates表中的重复问题。

+0

费率表中没有重复项。在dbo.5分钟里有重复的区域值:) – 2015-03-02 20:48:56

+2

@PrateekDaniels不,在dbo.Rates表中有** **重复的** – Lamak 2015-03-02 20:49:39

7

这不会解决你的问题,但使用updatejoin将使查询更容易一起工作:

update f 
    set ProfitUSD = Profit * coalesce(Rate, 1.0) 
    from dbo.[5 Mins] f left join 
     dbo.rates r 
     on f.Region = r.Region; 

请注意,这假定表格中没有其他区域可能匹配,但您的case未提及。这似乎是一个合理的假设。

该版本将起作用,因为它不会产生错误。问题在于一个或多个区域在rates中有一行或多行。你可以使用它们来找到它们:

select region, count(*) 
from dbo.rates r 
group by region 
having count(*) > 1; 

然后修复数据。或者决定你想要哪一行并修正你的逻辑。在这个版本中,逻辑将更容易修复。