【问题标题】:SQL adding row if it does not already exist如果行不存在,则 SQL 添加行
【发布时间】:2021-09-30 11:01:55
【问题描述】:

说我有这张桌子

Product Version Value
1 1 1000
2 1 2000
3_a 1 2500
3 1 3000
2_a 1 1200

我想做的是:
对于产品不以“_a”结尾的每一行:
检查是否有任何以“_a”结尾的具有相同产品编号的行:
检查该行中的值是否是第一行值的 60%。

如果没有这样的行,创建它并将版本增加 1

在逻辑上:

For each row {
    If exists(concatenate(row.Product,'_a')) as row2 then {
        If not(row2.Value=row.Value*0.6) then {
            Create row, Product=row2.Product, Version=row2.version+1,Value=row.value*0.6
        }
    } Else {
        Create row, Product=concatenate(row.Product,'_a'), Version=1,Value=row.value*0.6
    }
}

我已经尝试过这种方法(使用正确的语法),但不知道如何引用原始行来比较值或生成版本。期望的结果应该是两个新行:

Product Version Value
1_a 1 600
3_a 2 1800

使用 SQL Server 2005
产品可以有重复,只要版本号不同。

编辑:
如果以 '_a' 结尾的产品的最新版本具有不正确的值,我还想添加具有正确值的更新版本:

如果我有: |产品 |版本 |价值 | |---------|---------|--------| | 1 | 1 | 1000 | | 1_a | 1 | 600 | | 1_a | 2 | 1000 |

那我想补充一下: |产品 |版本 |价值 | |---------|---------|--------| | 1_a | 3 | 600 |

【问题讨论】:

  • 您使用的是旧的不受支持的 SQL Server 版本吗? (如果是这样,为什么?)。或者你在使用 MySQL 吗?这还不清楚。
  • 我确实在使用旧的、不受支持的 2005 版 SQL Server(我没有更新公司应用程序的权限/权限)。

标签: mysql sql sql-server-2005


【解决方案1】:

我用这个查询解决了这个问题。感谢 Serg 提供了一个良好的开端!

insert tbl(Product,Version,Value)
select concat(t1.product,'_a'),
    (select coalesce(max(t2.version),0) + 1
    from tbl t2
    where t2.Product = concat(t1.Product,'_a'),
    t1.value * 0.6
from tbl t1 inner join (
    select Product,max(Version) as Version
    from tbl
    group by Product
) as t3 on t3.Product = t1.Product and t3.Version = t1.Version
where right(t1.Product,2) != '_a'
    and not exists (select 1
        from tbl t4 inner join (
            select Product,max(Version) as Version
            from tbl
            group by Product
        ) as t5 on t5.Product = t4.Product and t5.Version = t4.Version
        where t4.Product = concat(t1.Product,'_a')
            and t4.Value = t1.Value * 0.6)

过于复杂?也许吧,但它有效。

【讨论】:

    【解决方案2】:

    试试

    insert tbl(Product, Version, Value)
    select concat(Product, '_a'), 
          (select coalesce(max(t2.Version), 0) + 1 
           from tbl t2 
           where t2.Product = concat(t1.Product, '_a')),
           t1.Value * 0.6
    from tbl t1
    where right(t1.Product, 2) !='_a' 
       and not exists (select 1 
           from tbl t3 
           where t3.Product = concat(t1.Product, '_a')
               and t3.Value = t1.Value * 0.6
               and t1.Version < t3.Version);
    

    编辑 不确定版本的错误值是什么。猜是版本等于或小于原版,所以加了and t1.Version &lt; t3.Version

    【讨论】:

    • 效果很好,非常感谢!我仍然有一个我没有指定的小问题,并且我无法自己解决。我已将要求添加到原始问题中。感谢您的帮助!
    • 这解决了问题,但又产生了另一个问题。在原始示例中,产品“2_a”的价值是产品“2”价值的 60%。但是这个查询仍然为 Product '2_a' 创建一个具有相同值的新行,并将 version 递增 1(每次运行查询时都会这样做,直到 Product '2_a' 的版本超过产品“2”(这会很快溢出数据库))。我想要的最后一行类似于“and t3.version = max(t3.version) group by Product”。希望这已经足够清楚了。
    • 我只想比较每个产品的最高版本。所有其他版本都可以视为存档版本。即使产品“a_1”派生自产品“a”,它们也应被视为单独的产品。所以我想: 1,找到产品'a'的最高版本。 2,找到产品'a_1'的最高版本。 3、比较他们的价值观。 4、如果'a_1'的值不是'a'的60%:s值。查询成功后添加新行。
    猜你喜欢
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2014-07-04
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多