SQL:如何用两个替换一个?

时间:2011-04-11 06:18:44

标签: sql sql-server-2005 replace line

我有一张桌子“第一”:

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 个答案:

答案 0 :(得分: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'

答案 1 :(得分:1)

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

答案 2 :(得分:1)

此解决方案可能非常具体,因为它意味着type列只能包含值HHMMVV

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')