2011-04-11 65 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

相关问题