我如何将列值与SQL中的行相加

问题描述 投票:-4回答:2

我有一个查询以对行的列值求和。下面是我的表xyz,并将brand and others列作为date.now,我想为每列汇总相同的brand值。

表xyz中的原始数据

enter image description here

Brands||01-Feb||02-Feb||03-Feb||04-Feb    
RAVEISJ||477||407||438|| 341    
RAVEISJ||265||264||206|| 320    
RAVEISJ||346||335||186|| 369    
RAVEISJ||492||358||462|| 387    
RAVEISJ||4271||4570||3200||5059    
JJAKH||4616||4738||3098||5065    
JJAKH||6861||5733||6652||5329    
JJAKH||7232||5944||6542||5257    
GO_GET||1312||2887||580||838    
GO_GET||1478||2776||535||731    
GO_GET||3851||1500||2192||5136    
GO_GET||4055||1543||2424||4914

我想要每个品牌的2月1日,2日,3日和4日的总和列

Brands||01-Feb||02-Feb||03-Feb||04-Feb    
GO_GET||10696||8706||5731||11619    
JJAKH||18709||16415||16292||15651    
RAVEISJ||5851||5934||4492||6476
mysql sql
2个回答
0
投票

这是一个聚合查询。您只需要:

select Brands,
       sum(`01-Feb`) as `01-Feb`,
       sum(`02-Feb`) as `02-Feb`,
       sum(`03-Feb`) as `03-Feb`,
       sum(`04-Feb`) as `04-Feb`
from xyz
group by Brands;

0
投票

标准化模型的示例:

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,brand VARCHAR(12) NOT NULL
,date DATE NOT NULL
,quantity INT NOT NULL
);

INSERT INTO my_table (brand,date,quantity) VALUES
('RAVEISJ','2020-02-01', 477), 
('RAVEISJ','2020-02-01', 265), 
('RAVEISJ','2020-02-01', 346), 
('RAVEISJ','2020-02-01', 492), 
('RAVEISJ','2020-02-01',4271), 
('JJAKH','2020-02-01',4616), 
('JJAKH','2020-02-01',6861), 
('JJAKH','2020-02-01',7232), 
('GO_GET','2020-02-01',1312), 
('GO_GET','2020-02-01',1478), 
('GO_GET','2020-02-01',3851), 
('GO_GET','2020-02-01',4055), 

('RAVEISJ','2020-02-02', 407), 
('RAVEISJ','2020-02-02', 264), 
('RAVEISJ','2020-02-02', 335), 
('RAVEISJ','2020-02-02', 358), 
('RAVEISJ','2020-02-02',4570), 
('JJAKH  ','2020-02-02',4738), 
('JJAKH  ','2020-02-02',5733), 
('JJAKH  ','2020-02-02',5944), 
('GO_GET ','2020-02-02',2887), 
('GO_GET ','2020-02-02',2776), 
('GO_GET ','2020-02-02',1500), 
('GO_GET ','2020-02-02',1543), 

('RAVEISJ','2020-02-03', 438), 
('RAVEISJ','2020-02-03', 206), 
('RAVEISJ','2020-02-03', 186), 
('RAVEISJ','2020-02-03', 462), 
('RAVEISJ','2020-02-03',3200), 
('JJAKH','2020-02-03',3098), 
('JJAKH','2020-02-03',6652), 
('JJAKH','2020-02-03',6542), 
('GO_GET','2020-02-03', 580), 
('GO_GET','2020-02-03', 535), 
('GO_GET','2020-02-03',2192), 
('GO_GET','2020-02-03',2424), 

('RAVEISJ','2020-02-04', 341), 
('RAVEISJ','2020-02-04', 320), 
('RAVEISJ','2020-02-04', 369), 
('RAVEISJ','2020-02-04', 387), 
('RAVEISJ','2020-02-04',5059), 
('JJAKH','2020-02-04',5065), 
('JJAKH','2020-02-04',5329), 
('JJAKH','2020-02-04',5257), 
('GO_GET','2020-02-04', 838), 
('GO_GET','2020-02-04', 731), 
('GO_GET','2020-02-04',5136), 
('GO_GET','2020-02-04',4914);

SELECT * FROM my_table;
+----+---------+------------+----------+
| id | brand   | date       | quantity |
+----+---------+------------+----------+
|  1 | RAVEISJ | 2020-02-01 |      477 |
|  2 | RAVEISJ | 2020-02-01 |      265 |
|  3 | RAVEISJ | 2020-02-01 |      346 |
|  4 | RAVEISJ | 2020-02-01 |      492 |
|  5 | RAVEISJ | 2020-02-01 |     4271 |
|  6 | JJAKH   | 2020-02-01 |     4616 |
|  7 | JJAKH   | 2020-02-01 |     6861 |
|  8 | JJAKH   | 2020-02-01 |     7232 |
|  9 | GO_GET  | 2020-02-01 |     1312 |
| 10 | GO_GET  | 2020-02-01 |     1478 |
| 11 | GO_GET  | 2020-02-01 |     3851 |
| 12 | GO_GET  | 2020-02-01 |     4055 |
| 13 | RAVEISJ | 2020-02-02 |      407 |
| 14 | RAVEISJ | 2020-02-02 |      264 |
| 15 | RAVEISJ | 2020-02-02 |      335 |
| 16 | RAVEISJ | 2020-02-02 |      358 |
| 17 | RAVEISJ | 2020-02-02 |     4570 |
| 18 | JJAKH   | 2020-02-02 |     4738 |
| 19 | JJAKH   | 2020-02-02 |     5733 |
| 20 | JJAKH   | 2020-02-02 |     5944 |
| 21 | GO_GET  | 2020-02-02 |     2887 |
| 22 | GO_GET  | 2020-02-02 |     2776 |
| 23 | GO_GET  | 2020-02-02 |     1500 |
| 24 | GO_GET  | 2020-02-02 |     1543 |
| 25 | RAVEISJ | 2020-02-03 |      438 |
| 26 | RAVEISJ | 2020-02-03 |      206 |
| 27 | RAVEISJ | 2020-02-03 |      186 |
| 28 | RAVEISJ | 2020-02-03 |      462 |
| 29 | RAVEISJ | 2020-02-03 |     3200 |
| 30 | JJAKH   | 2020-02-03 |     3098 |
| 31 | JJAKH   | 2020-02-03 |     6652 |
| 32 | JJAKH   | 2020-02-03 |     6542 |
| 33 | GO_GET  | 2020-02-03 |      580 |
| 34 | GO_GET  | 2020-02-03 |      535 |
| 35 | GO_GET  | 2020-02-03 |     2192 |
| 36 | GO_GET  | 2020-02-03 |     2424 |
| 37 | RAVEISJ | 2020-02-04 |      341 |
| 38 | RAVEISJ | 2020-02-04 |      320 |
| 39 | RAVEISJ | 2020-02-04 |      369 |
| 40 | RAVEISJ | 2020-02-04 |      387 |
| 41 | RAVEISJ | 2020-02-04 |     5059 |
| 42 | JJAKH   | 2020-02-04 |     5065 |
| 43 | JJAKH   | 2020-02-04 |     5329 |
| 44 | JJAKH   | 2020-02-04 |     5257 |
| 45 | GO_GET  | 2020-02-04 |      838 |
| 46 | GO_GET  | 2020-02-04 |      731 |
| 47 | GO_GET  | 2020-02-04 |     5136 |
| 48 | GO_GET  | 2020-02-04 |     4914 |
+----+---------+------------+----------+

...以及对该模型的查询...

SELECT brand,date,sum(quantity) total FROM my_table GROUP BY brand,date;
+---------+------------+-------+
| brand   | date       | total |
+---------+------------+-------+
| GO_GET  | 2020-02-01 | 10696 |
| GO_GET  | 2020-02-02 |  8706 |
| GO_GET  | 2020-02-03 |  5731 |
| GO_GET  | 2020-02-04 | 11619 |
| JJAKH   | 2020-02-01 | 18709 |
| JJAKH   | 2020-02-02 | 16415 |
| JJAKH   | 2020-02-03 | 16292 |
| JJAKH   | 2020-02-04 | 15651 |
| RAVEISJ | 2020-02-01 |  5851 |
| RAVEISJ | 2020-02-02 |  5934 |
| RAVEISJ | 2020-02-03 |  4492 |
| RAVEISJ | 2020-02-04 |  6476 |
+---------+------------+-------+

处理应用程序代码中所有剩余的数据显示问题。

热门问题
推荐问题
最新问题