SQL: как заменить один на два? - PullRequest
2 голосов
/ 11 апреля 2011

У меня есть таблица "first":

enter image description here

Я пытаюсь написать запрос.

    If first.type = 'MM'

    then replace this line in two new lines

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

Например: строка, где первый.type = 'MM':

enter image description here

Я хочу заменить эту строку следующим образом:

enter image description here

И результат:

enter image description here

Кто-нибудь имеет представление о том, как это можно сделать?

Я использую MS SQL Server Management Studio Express.

Ответы [ 3 ]

3 голосов
/ 11 апреля 2011

select id,freq,parity,line,type,gain,obdim from [first] where type&lt&gt'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 голос
/ 11 апреля 2011

Это решение, вероятно, очень специфично в том смысле, что оно подразумевает, что столбец type может иметь только значения HH, MM или 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')
.
1 голос
/ 11 апреля 2011
select id,frequency, 'VV' type --, .. 
from table1
where type='MM'
union all
select id,frequency, 'HH' as type --, .. 
from table1
where type='MM'
...