首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SQL delete最小值加上组中的下一个最大值

SQL delete最小值加上组中的下一个最大值
EN

Stack Overflow用户
提问于 2015-07-22 00:28:45
回答 2查看 66关注 0票数 1
代码语言:javascript
运行
复制
What I get                  What I need

id  hash    assigned        id  hash    assigned
--  ----    --------        --  ----    --------
1   1j      1               1   1j      1
2   1j-1    1               3   1j-2    1
3   1j-2    1               4   3m      1
4   3m      1               6   3m-8    1
5   3m-3    1               7   f7      1
6   3m-8    1               8   6q      0
7   f7      1               9   3y      0
8   6q      0               10  3y-3    0
9   3y      0               
10  3y-3    0               

我的数据集看起来像这样。散列始终是一个特定的数字长度,在本例中为2个字符。它在与顺序的但不是相同数量的单元的扩展同步后被修改。我需要删除那些分配了1的第一个修改。

这是我的非工作最佳猜测:

代码语言:javascript
运行
复制
DELETE 
  FROM `table` 
  WHERE (`assigned`=1) 
    IN (SELECT `hash`, min(`hash`)+1 FROM `table` GROUP BY SUBSTRING(`hash`,1 , 2) ORDER BY hash ASC;
EN

回答 2

Stack Overflow用户

发布于 2015-07-22 00:52:16

一旦你的计数器达到两位数和三位数,你在字符串比较方面就会遇到问题。这是一个直截了当的方法,可能会让你更接近。在实际删除所有内容之前,请记住尝试使用select

代码语言:javascript
运行
复制
delete from T
where
    assigned = 1
    -- Business requirement not explained.
    -- I'm assuming the entire group has the same assigned value.

    and hash = (
        select min(hash) from T as t2
            where 
                    left(t2.hash, 2) = left(T.hash, 2)
                and t2.hash > (
                    select min(hash) from T as t3
                    where left(t3.hash, 2) = left(t2.hash, 2)
                )
    )
票数 0
EN

Stack Overflow用户

发布于 2015-07-22 00:58:15

SQL Server

代码语言:javascript
运行
复制
delete from T where id in (
    select 
       t1.id
    from T t1
    where t1.assigned = 1 and t1.hash like '%-%' and not exists (
      select * from T t2 where left(t1.hash, 2) = left(t2.hash, 2) and t2.hash like '%-%' and
            cast(substring(t2.hash, 4, len(t2.hash) - 3) as int) <
            cast(substring(t1.hash, 4, len(t1.hash) - 3) as int)
    )
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31544654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档