2011-04-11 68 views
2

我有一個表 「第一」:SQL:如何用兩個替換一個?

enter image description here

我想編寫一個查詢。

If first.type = 'MM' 

    then replace this line in two new lines 

    where first.type = 'HH' and first.type = 'VV'. 

例如:線,其中first.type = 'MM':

enter image description here

我要替換這一行這樣的:

enter image description here

而結果:

enter image description here

有人對此有何看法?

我正在使用MS SQL Server Management Studio Express。

回答

3
 

select id,freq,parity,line,type,gain,obdim from [first] where type<>'MM' 
union 
select id,freq,parity,line,'HH' as type,gain,obdim from [first] where type='MM' 
union 
select id,freq,parity,line,'VV' as type,gain,obdim from [first] where type='MM' 
 
+0

+1,但請將*擴展到實際列中。這張桌子得到更新後不久,所有東西都分崩離析。 – Dane 2011-04-11 06:59:32

+0

謝謝。這解決了我的問題。 – Juronis 2011-04-12 06:57:12

1
select id,frequency, 'VV' type --, .. 
from table1 
where type='MM' 
union all 
select id,frequency, 'HH' as type --, .. 
from table1 
where type='MM' 
1

這種解決方案可能是在這個意義上非常特殊,它意味着type列只能有值HHMM,或VV

SELECT 
    f.ID, 
    f.freq, 
    f.parity, 
    f.line, 
    t.type, 
    f.gain, 
    f.obdim 
FROM [first] f 
    INNER JOIN (
    SELECT 'HH' AS type 
    UNION ALL 
    SELECT 'VV' 
) t ON f.type IN (t.type, 'MM') 
+0

我無法運行此示例。我在「t(type)」上遇到錯誤。 – Juronis 2011-04-11 10:27:35

+0

@Juronis:不知道爲什麼會有這樣的錯誤。不過,我已經稍微改變了它,現在應該沒問題。還要感謝您的評論,我注意到我的解決方案中有一個錯誤。修正了它。 – 2011-04-11 11:46:38

+0

嗯,仍然不起作用。我收到錯誤「查詢輸入必須包含至少一個表或查詢」。 – Juronis 2011-04-12 05:42:06

相關問題