Это данные базы данных.

Name   id  Col1  Col2  Col3 Col4 Total  Balance
Row1   1    6     1     A     Z     -      - 
Row2   2    2     3     B     Z     -      - 
Row3   3    9     5     B     Y     -      - 
Row4   4    16    8     C     Y     -      -

Я хочу обновить столбцы «Итого» и «Баланс» со строки 2 до строки 4 с условием. Вот логика суммирования итогового столбца:

обновить Total = Col1 + Col2, если Col3 = A и Col4 & lt; & gt; Z
ИЛИ
Итого = Col1-Col2, если Col3 = B и Col4 & lt; & gt; Z
ИЛИ
Итого = Col1 * Col2, если Col3 = C и Col4 & lt; & gt; Z

И также обновите сумму баланса,

баланс = предыдущая строка баланса + текущая строка итога

2
Lim Neo 26 Фев 2016 в 12:01

4 ответа

Лучший ответ

Вот решение с помощью одной пользовательской переменной.

Результат подтвержден полной демонстрацией.

SQL:

-- data preparation for demo
create table tbl(Name char(100), id int, Col1 int, Col2 int, Col3 char(20), Col4 char(20), Total int, Balance int);
insert into tbl values
('Row1',1,6,1,'A','Z',0,0),
('Row2',2,2,3,'B','Z',0,0),
('Row3',3,9,5,'B','Y',0,0),
('Row4',4,12,8,'C','Y',0,0);
SELECT * FROM tbl;

-- Query needed
SET @bal = 0;
UPDATE tbl
SET
    Total = CASE    WHEN Col3 = 'A' and Col4 <> 'Z'
                        THEN Col1+Col2
                    WHEN Col3 = 'B' and Col4 <> 'Z'
                        THEN Col1-Col2
                    WHEN Col3 = 'C' and Col4 <> 'Z'
                        THEN Col1*Col2
                    ELSE 0 END,
    Balance = (@bal:=@bal + Total);
SELECT * FROM tbl;

Результат (как и ожидалось):

mysql> SELECT * FROM tbl;
+------+------+------+------+------+------+-------+---------+
| Name | id   | Col1 | Col2 | Col3 | Col4 | Total | Balance |
+------+------+------+------+------+------+-------+---------+
| Row1 |    1 |    6 |    1 | A    | Z    |     0 |       0 |
| Row2 |    2 |    2 |    3 | B    | Z    |     0 |       0 |
| Row3 |    3 |    9 |    5 | B    | Y    |     0 |       0 |
| Row4 |    4 |   12 |    8 | C    | Y    |     0 |       0 |
+------+------+------+------+------+------+-------+---------+
4 rows in set (0.00 sec)

mysql> -- Query needed
mysql> SET @bal = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> UPDATE tbl
    -> SET
    ->     Total = CASE    WHEN Col3 = 'A' and Col4 <> 'Z'
    ->                         THEN Col1+Col2
    ->                     WHEN Col3 = 'B' and Col4 <> 'Z'
    ->                         THEN Col1-Col2
    ->                     WHEN Col3 = 'C' and Col4 <> 'Z'
    ->                         THEN Col1*Col2
    ->                     ELSE 0 END,
    ->     Balance = (@bal:=@bal + Total);
Query OK, 2 rows affected (0.00 sec)
Rows matched: 4  Changed: 2  Warnings: 0

mysql>
mysql> SELECT * FROM tbl;
+------+------+------+------+------+------+-------+---------+
| Name | id   | Col1 | Col2 | Col3 | Col4 | Total | Balance |
+------+------+------+------+------+------+-------+---------+
| Row1 |    1 |    6 |    1 | A    | Z    |     0 |       0 |
| Row2 |    2 |    2 |    3 | B    | Z    |     0 |       0 |
| Row3 |    3 |    9 |    5 | B    | Y    |     4 |       4 |
| Row4 |    4 |   12 |    8 | C    | Y    |    96 |     100 |
+------+------+------+------+------+------+-------+---------+
4 rows in set (0.00 sec)
3
Dylan Su 26 Фев 2016 в 09:59
SET @prev_id = 1;
UPDATE tableName SET 
Total =  
CASE
  WHEN Col3 = A and Col4 <> Z THEN Col1 + Col2
  WHEN Col3 = B and Col4 <> Z THEN Col1 - Col2
  WHEN Col3 = C and Col4 <> Z THEN Col1 * Col2
END,
Balance = 
CASE
  WHEN Col3 = A and Col4 <> Z THEN 
  (SELECT Balance FROM tableName WHERE id = @prev_id) + (Col1 + Col2)
  WHEN Col3 = B and Col4 <> Z THEN 
  (SELECT Balance FROM tableName WHERE id = @prev_id) + (Col1 - Col2)
  WHEN Col3 = C and Col4 <> Z THEN 
  (SELECT Balance FROM tableName WHERE id = @prev_id) + (Col1 * Col2)
END,
@prev_id := id

WHERE t1.id > 1;
1
user5961012user5961012 26 Фев 2016 в 09:49
UPDATE tableName t1 SET 
Total =  
CASE
  WHEN t1.Col3 = "A" and t1.Col4 <> "Z" THEN t1.Col1 + t1.Col2
  WHEN t1.Col3 = "B" and t1.Col4 <> "Z" THEN t1.Col1 - t1.Col2
  WHEN t1.Col3 = "C" and t1.Col4 <> "Z" THEN t1.Col1 * t1.Col2
END
Balance = 
CASE
  WHEN t1.Col3 = "A" and t1.Col4 <> "Z" THEN 
  (SELECT t2.Balance FROM tableName t2 
    WHERE t2.id = (select max(t3.id) from tableName t3 where t3.id < t1.id))
    + (t1.Col1 + t1.Col2)
  WHEN t1.Col3 = "B" and t1.Col4 <> "Z" THEN 
  (SELECT t2.Balance FROM tableName t2 
    WHERE t2.id = (select max(t3.id) from tableName t3 where t3.id < t1.id))
    + (t1.Col1 - t1.Col2)
  WHEN t1.Col3 = "C" and t1.Col4 <> "Z" THEN 
  (SELECT t2.Balance FROM tableName t2 
    WHERE t2.id = (select max(t3.id) from tableName t3 where t3.id < t1.id))
    + (t1.Col1 * t1.Col2)
END
WHERE t1.id > 1;
1
mmuzahid 26 Фев 2016 в 09:38

Попробуй это

 SET @old_balance = 0;
    UPDATE tableName SET 
    Total =  
    CASE
      WHEN Col3 = 'A' and Col4 <> 'Z' THEN Col1 + Col2
      WHEN Col3 = 'B' and Col4 <> 'Z' THEN Col1 - Col2
      WHEN Col3 = 'C' and Col4 <> 'Z' THEN Col1 * Col2
    END,
    Balance = 
    CASE
      WHEN Col3 = 'A' and Col4 <> 'Z' THEN 
      @old_balance + (Col1 + Col2)
      WHEN Col3 = 'B' and Col4 <> 'Z' THEN 
      @old_balance + (Col1 - Col2)
      WHEN Col3 = 'C' and Col4 <> 'Z' THEN 
      @old_balance + (Col1 * Col2)
    END,
    @old_balance := 
    CASE
      WHEN Col3 = 'A' and Col4 <> 'Z' THEN 
      @old_balance + (Col1 + Col2)
      WHEN Col3 = 'B' and Col4 <> 'Z' THEN 
      @old_balance + (Col1 - Col2)
      WHEN Col3 = 'C' and Col4 <> 'Z' THEN 
      @old_balance + (Col1 * Col2)
    END
    WHERE t1.id > 1;
1
Vipin Jain 26 Фев 2016 в 09:51