0

## 關於SQL When...then...語法問題

select
case
when TABLE.currency_id='USD' then '1'
when TABLE.currency_id='RMB' then 'A'
end "匯率",

### 4 個回答

6
kissingboy
iT邦新手 4 級 ‧ 2012-12-12 10:31:27

``````&lt;pre class="c" name="code">
select
case
when TABLE.currency_id='USD' then '1'
when TABLE.currency_id='RMB' then [TABLE].[A]
end "匯率"
FROM TABLE
``````

``````&lt;pre class="c" name="code">
select
case
when TABLE.currency_id='USD' then '1'
when TABLE.currency_id='RMB' then (SELECT TOP 1 A FROM [TABLE_B])
end "匯率"
FROM TABLE
``````

4

iT邦高手 1 級 ‧ 2012-12-11 16:50:27

case when else end只能在同資料表內查詢~

select
(case when currency_id = 'USD' then '1' else 'A' end) as 匯率
from table1

4
wiseguy
iT邦超人 1 級 ‧ 2012-12-11 21:46:25

``````&lt;pre class="c" name="code">
select 1 as 匯率 from TABLE where currency_id='USD'
union
select A欄位 as 匯率 from TABLE where currency_id='RMB'
``````
4
summertw
iT邦好手 1 級 ‧ 2012-12-12 09:24:58

select
case
when TABLE.currency_id='USD' then '1'
when TABLE.currency_id='RMB' then 'A'
end "匯率",
...

select
case
when TABLE.currency_id='USD' then '1'
when TABLE.currency_id='RMB' then [TABLE].[ColumnA]
end AS 匯率 ,
...