SQL1更新column3 group1 by column1并比较column2中的值

时间:2017-09-07 18:01:12

标签: mysql sql

如果我有一个如下所示的表T:其中id是唯一的自动增量主键。差异列默认为0.我想在每个id_str组中仅更新maximumId - secondLargestId的差异,而其余的保持不变。

template<typename F, typename T>
auto dispatch(F && f, T && t, std::size_t i) -> decltype((std::forward<F>(f)(std::forward<T>(t)),0)) {
    std::forward<F>(f)(std::forward<T>(t));
    return 0;
}

template<typename F, typename T>
auto dispatch(F && f, T && t, std::size_t i) -> decltype((std::forward<F>(f)(std::forward<T>(t), i),0)) {
    std::forward<F>(f)(std::forward<T>(t),i);
    return 0;
}

我想要的结果是:

(^|\s)#([\w]+)($|\s)

如何编写查询?

2 个答案:

答案 0 :(得分:0)

这应该可以解决MySQL问题。

CREATE TABLE SomeTable 
  ( id_str VARCHAR(10),
    id INTEGER,
    value_ DECIMAL(7,5),
    difference DECIMAL(7,5)
  );


INSERT INTO SomeTable VALUES(2380,1,21.01,0);
INSERT INTO SomeTable VALUES(2380,3,22.04,0);
INSERT INTO SomeTable VALUES(2380,5,22.65,0);
INSERT INTO SomeTable VALUES(2380,8,23.11,0);
INSERT INTO SomeTable VALUES(2380,10,35.21,0);
INSERT INTO SomeTable VALUES(20100,2,37.07,0);
INSERT INTO SomeTable VALUES(20100,4,38.17,0);
INSERT INTO SomeTable VALUES(20100,6,38.97,0);
INSERT INTO SomeTable VALUES(20103,7,57.98,0);
INSERT INTO SomeTable VALUES(20103,9,60.83,0);

UPDATE SomeTable,
       (SELECT T1.id AS id_updt,
               T1.value_ - T2.value_ AS diff_updt
          FROM (SELECT id_str,
                       id,
                       value_,
                       (
                       CASE id_str
                           WHEN @curStr THEN @curRow := @curRow + 1
                           ELSE @curRow := 1 
                            AND @curStr := id_str
                        END
                       ) AS rnk

                  FROM SomeTable,
                       (SELECT @curRow := 0, @curStr := '') r
                 ORDER
                    BY id_str DESC,
                       id DESC
               ) AS T1
         INNER 
          JOIN (SELECT id_str,
                       id,
                       value_,
                       (
                       CASE id_str
                           WHEN @curStr THEN @curRow := @curRow + 1
                           ELSE @curRow := 1 
                            AND @curStr := id_str
                        END
                       ) AS rnk

                  FROM SomeTable,
                       (SELECT @curRow := 0, @curStr := '') r
                 ORDER
                    BY id_str DESC,
                       id DESC
               ) AS T2     
            ON T1.id_str = T2.id_str
           AND T1.rnk = 1
           AND T2.rnk = 2
       ) AS UPDT
   SET SomeTable.difference = UPDT.diff_updt
 WHERE SomeTable.id = UPDT.id_updt;

不推荐使用的解决方案 - 这适用于支持排名功能的DBMS。

UPDATE SomeTable
  FROM ( SELECT RNK1.id AS id_updt,
                RNK1.value_ - RNK2.value_ AS diff_updt
           FROM (SELECT id_str,
                        RANK() OVER
                           ( PARTITION BY id_str
                                 ORDER BY id DESC
                           ) AS id_rnk
                  FROM SomeTable
                ) AS RNK1
          INNER
           JOIN (SELECT id_str,
                        RANK() OVER
                           ( PARTITION BY id_str
                                 ORDER BY id DESC
                           ) - 1 AS id_rnk_decrement
                  FROM SomeTable
                ) AS RNK2
             ON RNK1.id_str = RNK2.id_str
            AND RNK1.id_rnk = RNK2.id_rnk_decrement
          WHERE RNK1.id_rnk = 1
       ) AS UPDT
   SET SomeTable.difference_ = UPDT.diff_updt
 WHERE SomeTable.id = UPDT.id_updt;

答案 1 :(得分:0)

您可以使用以下查询找到每个组的两个最大ID:

select t1.id_str, max(t1.id) as id1, (
    select max(t2.id)
    from mytable t2
    where t2.id_str = t1.id_str
      and t2.id < max(t1.id)
) as id2
from mytable t1
group by t1.id_str;

结果:

| id_str | id1 | id2 |
|--------|-----|-----|
|   2380 |  10 |   8 |
|  20100 |   6 |   4 |
|  20103 |   9 |   7 |

在更新语句中将其用作子查询:

update mytable u
join (
    select t1.id_str, max(t1.id) as id1, (
        select max(t2.id)
        from mytable t2
        where t2.id_str = t1.id_str
          and t2.id < max(t1.id)
    ) as id2
    from mytable t1
    group by t1.id_str
) t on t.id1 = u.id
join mytable t1 on  t1.id = t.id1
join mytable t2 on  t2.id = t.id2
set u.Difference = t1.Value - t2.Value;

该表现在将包含:

| id_str | id | Value | Difference |
|--------|----|-------|------------|
|   2380 |  1 | 21.01 |          0 |
|   2380 |  3 | 22.04 |          0 |
|   2380 |  5 | 22.65 |          0 |
|   2380 |  8 | 23.11 |          0 |
|   2380 | 10 | 35.21 |       12.1 |
|  20100 |  2 | 37.07 |          0 |
|  20100 |  4 | 38.17 |          0 |
|  20100 |  6 | 38.97 |        0.8 |
|  20103 |  7 | 57.98 |          0 |
|  20103 |  9 | 60.83 |       2.85 |

http://rextester.com/CCO40873