首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >修改SQL表以压缩相似的行,同时汇总列

修改SQL表以压缩相似的行,同时汇总列
EN

Stack Overflow用户
提问于 2018-01-14 11:42:21
回答 3查看 190关注 0票数 0

这里有一个问题,是我所要做的工作的总体要点:

Sum values from multiple rows into one row

然而,据我所知,我正在寻求进一步的功能,这将永久地修改所讨论的表,使之看起来类似于在另一个线程中建议的SELECT语句的结果。

所以桌子:

代码语言:javascript
运行
复制
Sales
--------------------------------------
account    product    qty    amount
--------------------------------------
01010      bottle     10     200
01010      bottle     20     100
01010      bottle     5      10
11111      can        50     200
11111      can        25     150

...would被永久修改如下所示

代码语言:javascript
运行
复制
Sales
--------------------------------------
account    product    qty    amount
--------------------------------------
01010      bottle     35     310
11111      can        75     350

正如链接中所回答的那样,使用带有SUM和GROUP的SELECT可以显示表需要什么样的外观,但是如何将这些更改实际应用到sales表中呢?

编辑:此查询将在每次向系统中添加新的一批销售时运行。它的目的是清理销售表后,新的记录已经添加。

替代方法

销售中的新记录是从不同的表中插入的,使用如下所示:

代码语言:javascript
运行
复制
"INSERT INTO sales
    SELECT account, product, qty, amount
    FROM new_sales;"

如果有一种方法来处理上一次插入期间的求和,而不是首先添加重复行,那也是可以接受的。请记住,这个解决方案仍然需要用于销售中没有现有重复行的新记录。

编辑:面向后代

一般的反应似乎是,我的最初方法是不可能的--除非创建一个带有CREATE和SELECT的temp_sales表,然后彻底清除销售,然后将temp_sales的内容复制到已清除的sales表中,然后截断temp_sales以供将来使用。

接受的解决方案使用了我也提到过的“替代方法”。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-01-14 12:46:43

假设new_sales在sales更新后被截断,然后开始重新填充,您可以使用insert..on重复的key..update。

代码语言:javascript
运行
复制
MariaDB [sandbox]> drop table if exists t,t1;
Query OK, 0 rows affected (0.20 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> create table t
    -> (account varchar(5),    product varchar(20),    qty  int default 0,  amount int default 0);
Query OK, 0 rows affected (0.16 sec)

MariaDB [sandbox]> create table t1
    -> (account varchar(5),    product varchar(20),    qty  int default 0,  amount int default 0);
Query OK, 0 rows affected (0.24 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> alter table t
    -> add unique key k1(account,product);
Query OK, 0 rows affected (0.15 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> truncate table t1;
Query OK, 0 rows affected (0.23 sec)

MariaDB [sandbox]> insert into t1 values
    -> ('01010'  ,    'bottle'   ,  10   ,  200),
    -> ('01010'  ,    'bottle'   ,  20   ,  100),
    -> ('01010'  ,    'bottle'   ,  5    ,  10),
    -> ('11111'  ,    'can'      ,  50   ,  200),
    -> ('11111'  ,    'can'      ,  25   ,  150);
Query OK, 5 rows affected (0.02 sec)
Records: 5  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> truncate table t;
Query OK, 0 rows affected (0.28 sec)

MariaDB [sandbox]> insert into t
    -> select account,product,t1qty,t1amount
    -> from
    -> (
    -> select t1.account,t1.product,sum(t1.qty) t1qty,sum(t1.amount) t1amount from t1 group by t1.account,t1.product
    -> ) s
    -> on duplicate key
    -> update qty = t.qty + t1qty, amount = t.amount + t1amount;
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> truncate table t1;
Query OK, 0 rows affected (0.32 sec)

MariaDB [sandbox]> insert into t1 values
    -> ('01010'  ,    'bottle'   ,  10   ,  200),
    -> ('01011'  ,    'bottle'   ,  20   ,  100),
    -> ('01011'  ,    'bottle'   ,  5    ,  10),
    -> ('11111'  ,    'can'      ,  50   ,  200),
    -> ('11111'  ,    'can'      ,  25   ,  150);
Query OK, 5 rows affected (0.02 sec)
Records: 5  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> insert into t
    -> select account,product,t1qty,t1amount
    -> from
    -> (
    -> select t1.account,t1.product,sum(t1.qty) t1qty,sum(t1.amount) t1amount from t1 group by t1.account,t1.product
    -> ) s
    -> on duplicate key
    -> update qty = t.qty + t1qty, amount = t.amount + t1amount;
Query OK, 5 rows affected (0.02 sec)
Records: 3  Duplicates: 2  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]>
MariaDB [sandbox]> select * from t;
+---------+---------+------+--------+
| account | product | qty  | amount |
+---------+---------+------+--------+
| 01010   | bottle  |   45 |    510 |
| 11111   | can     |  150 |    700 |
| 01011   | bottle  |   25 |    110 |
+---------+---------+------+--------+
3 rows in set (0.00 sec)

MariaDB [sandbox]>
票数 0
EN

Stack Overflow用户

发布于 2018-01-14 11:54:53

可以从select语句创建表。

所以你可以做这样的事:

代码语言:javascript
运行
复制
create table sales_sum as 
  select 
     account,
     product,
     sum(qty),
     sum(amount) 
   from 
     sales
   group by 
     account, 
     product

这将创建一个具有正确结构的表,并将插入您希望拥有的记录。当然,您可以调整查询或表名。

票数 0
EN

Stack Overflow用户

发布于 2018-01-14 12:16:25

此查询执行ETL工具所能完成的任务,但您需要运行整个脚本:

-旧桌子

代码语言:javascript
运行
复制
CREATE TABLE yourtable (
  [state] varchar(2),
  [month] varchar(7),
  [ID] int,
  [sales] int
)
;
INSERT INTO yourtable ([state], [month], [ID], [sales])
  VALUES ('FL', 'June', 0001, '12000'),
  ('FL', 'June', 0001, '6000'),
  ('FL', 'June', 0001, '3000'),
  ('FL', 'July', 0001, '6000'),
  ('FL', 'July', 0001, '4000'),
  ('TX', 'January', 0050, '1000'),
  ('MI', 'April', 0032, '5000'),
  ('MI', 'April', 0032, '8000'),
  ('CA', 'April', 0032, '2000');

SELECT
  state,
  month,
  id,
  SUM(sales) Total
FROM yourtable
GROUP BY state,
         month,
         id;

-----Creating new table from old table

CREATE TABLE yourtable1 (
  [state] varchar(2),
  [month] varchar(7),
  [ID] int,
  [sales] int
)
;

----Inserting aggregation logic

INSERT INTO yourtable1 (state, month, id, sales)
  SELECT
    state,
    month,
    id,
    SUM(sales)
  FROM yourtable
  GROUP BY state,
           month,
           id;
-----Fetching records

SELECT
  *
FROM yourtable1;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48249204

复制
相关文章

相似问题

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