[英]Make table with two new columns that categorize another column by even or odd and that sums all even and odd numbers in a category
I have the following structure of table:我有以下表结构:
category类别 | user_id用户身份 | value价值 |
---|---|---|
A一种 | 1 1个 | 0.01 0.01 |
A一种 | 2 2个 | 0.05 0.05 |
A一种 | 3 3个 | 0.09 0.09 |
A一种 | 4 4个 | 0.12 0.12 |
B乙 | 1 1个 | 0.34 0.34 |
B乙 | 2 2个 | 0.27 0.27 |
B乙 | 3 3个 | 0.08 0.08 |
B乙 | 4 4个 | 0.12 0.12 |
There are many more rows in the actual table.实际表中还有更多行。 This is just an example.这只是一个例子。
I want to make a table that keeps 'category', makes another column 'user_id_type' that labels even and odd, and another new column (value_sum) that sums all of the 'value' based on 'category' and 'user_id_type'.我想制作一个保留“类别”的表,制作另一列标记偶数和奇数的“user_id_type”,以及另一个新列(value_sum),它根据“类别”和“user_id_type”对所有“值”求和。
So, it will have only four rows, with 'A' 'odd' and the sum, 'A' 'even' and the sum, 'B' 'odd' and the sum, 'B' 'even' and the sum.因此,它只有四行,“A”“奇数”和总和,“A”“偶数”和总和,“B”“奇数”和总和,“B”“偶数”和总和。
I want it to look like this:我希望它看起来像这样:
category类别 | user_id_type user_id_type | value_sum价值总和 |
---|---|---|
A一种 | odd奇怪的 | 0.10 0.10 |
A一种 | even甚至 | 0.17 0.17 |
B乙 | odd奇怪的 | 0.42 0.42 |
B乙 | even甚至 | 0.39 0.39 |
Schema:架构:
CREATE TABLE table_1 (
`category` VARCHAR(2),
`user_id` INT(2),
`value` DECIMAL(3,2)
);
INSERT INTO table_1
(`category`, `user_id`, `value`)
VALUES
('A', 1, 0.01),
('A', 2, 0.05),
('A', 3, 0.09),
('A', 4, 0.12),
('B', 1, 0.34),
('B', 2, 0.27),
('B', 3, 0.08),
('B', 4, 0.12)
;
You can use MOD
with a case
expression to determine odd or even, and then use the results as a derived table to aggregate from:您可以将MOD
与case
表达式一起使用来确定奇数或偶数,然后将结果用作派生表以从中聚合:
select a.category
, a.user_id_type
, sum(a.value)
from
(
SELECT t.category
, case
when MOD(t.user_id, 2) = 0 then 'Even'
else 'Odd'
end user_id_type
, t.value
FROM tbl t
) a
group by a.category, a.user_id_type
;
Edit: Adding Lemon's recommendation for compactness编辑:添加 Lemon 对紧凑性的建议
Here is the example with Lemon's compacted version:这是 Lemon 压缩版本的示例:
select category
, user_id_type
, sum(value)
from
(
SELECT category
, IF(MOD(t.user_id, 2)=0, 'even', 'odd') user_id_type
, value
FROM tbl t
) a
group by a.category, a.user_id_type
;
You can create a table according to your requirements and fill this table using a select like this:您可以根据您的要求创建一个表,并使用 select 填充此表,如下所示:
INSERT INTO table_2
SELECT category,
CASE WHEN user_id % 2 = 0 THEN 'even' ELSE 'odd' END user_id_type,
SUM(value) FROM table_1 GROUP BY user_id_type, category ORDER BY category;
Please have a look here to see it's working: fiddle请在这里查看它是否正常工作: fiddle
You can use MODulo to dete4rmin the odds您可以使用 MODulo 来确定赔率
CREATE TABLE table_1 ( `category` VARCHAR(2), `user_id` INT(2), `value` DECIMAL(3,2) ); INSERT INTO table_1 (`category`, `user_id`, `value`) VALUES ('A', 1, 0.01), ('A', 2, 0.05), ('A', 3, 0.09), ('A', 4, 0.12), ('B', 1, 0.34), ('B', 2, 0.27), ('B', 3, 0.08), ('B', 4, 0.12);
SELECT `category`, MAX(CASE WHEN MOD( `user_id`,2) = 1 then 'Even' ELSE 'Odd' ENd ) odds, SUM(`value`) FROM table_1 GROUP BY category, MOD( `user_id`,2) ORDER BY category, MOD( `user_id`,2) ASC
category |类别 | odds |赔率 | SUM(`value`):------- |:--- | SUM(`value`):------- |:--- | -----------: A | ----------:一个| Odd |奇 | 0.17 A | 0.17 一个 | Even |甚至 | 0.10 B | 0.10 乙 | Odd |奇 | 0.39 B | 0.39 乙 | Even |甚至| 0.42 0.42
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.