SQL:如何将一替换为二?

发布于 2024-10-31 09:29:57 字数 603 浏览 4 评论 0原文

我有一个表“第一个”:

在此处输入图像描述

我正在尝试编写一个查询。

    If first.type = 'MM'

    then replace this line in two new lines

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

例如:first.type = 'MM':

在此处输入图像描述

我想像这样替换这一行:

在此处输入图像描述

结果:

在此处输入图像描述

有人知道如何做到这一点吗?

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

I have a table "first":

enter image description here

I am trying to write a query.

    If first.type = 'MM'

    then replace this line in two new lines

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

For example : The line where first.type = 'MM':

enter image description here

I want to replace this line like that:

enter image description here

And the result:

enter image description here

Does someone have an idea on how this can be done?

I'm using MS SQL Server Management Studio Express.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

汹涌人海 2024-11-07 09:29:57

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'


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'

南…巷孤猫 2024-11-07 09:29:57
select id,frequency, 'VV' type --, .. 
from table1
where type='MM'
union all
select id,frequency, 'HH' as type --, .. 
from table1
where type='MM'
select id,frequency, 'VV' type --, .. 
from table1
where type='MM'
union all
select id,frequency, 'HH' as type --, .. 
from table1
where type='MM'
夏了南城 2024-11-07 09:29:57

该解决方案可能非常具体,因为它意味着 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')

This solution is probably very specific in the sense that it implies that the type column can only have values HH, MM, or 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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文