如何根据重复的行列值添加列值?
How do I add up values for columns based on repeating row column value?
我使用 sql 从数据库中选择了 table 个值。
SELECT
a.Chapter, a.price, b.book
FROM
Table1 a, Table2 b, Table3 c
WHERE
a.id = c.id and b.id = c.id2
GROUP BY
a.Chapter, a.price
ORDER BY
a.Chapter
它带回的值是:
|Chapter|Price|book|
|_______|_____|____|
|01 | 25 |eg1 |
|01 | 23 |eg2 |
|01 | 9 |eg3 |
|02 | 15 |eg4 |
|02 | 89 |eg5 |
|02 | 11 |eg6 |
|02 | 3 |eg7 |
|02 | 25 |eg8 |
我如何获取每个重复的 01 的价格(顺便说一句,它是字符串格式,因为它不是一本书的章节)并将它们加在一起并使它们成为新价格?例如:
Chapter Price
01 57
02 143
The book part isn't really important at the moment so it can be excluded
你试过 DISTINCT 了吗?
SELECT DISTINCT x
from table1
where example = example2
SELECT a.Chapter, sum(a.price)
FROM Table1 a
INNER JOIN Table3 c on c.id = a.id
INNER JOIN Table2 b on b.id = c.id
GROUP BY
a.Chapter
ORDER BY
a.Chapter
你的做法是错误的。你按章节和价格分组,但你也 select 这本书。这本书将是任意选择的章节和价格匹配的书籍之一。所以有了这个数据
book chapter price
eg1 01 25
eg1 02 30
eg2 01 25
eg3 01 30
你可能会得到
book chapter price
eg1 01 25
eg1 02 30
eg3 01 30
从而抑制图书eg2.
无论如何,GROUP BY chapter
每章都有一个结果行。您得到 SUM
.
的总和
select chapter, sum(price)
from chapters
group by chapter;
order by chapter;
只用一个table就可以实现
SELECT
a.Chapter, SUM(a.price) as price
FROM
Table1 a
GROUP BY
a.Chapter
ORDER BY
a.Chapter
我使用 sql 从数据库中选择了 table 个值。
SELECT
a.Chapter, a.price, b.book
FROM
Table1 a, Table2 b, Table3 c
WHERE
a.id = c.id and b.id = c.id2
GROUP BY
a.Chapter, a.price
ORDER BY
a.Chapter
它带回的值是:
|Chapter|Price|book|
|_______|_____|____|
|01 | 25 |eg1 |
|01 | 23 |eg2 |
|01 | 9 |eg3 |
|02 | 15 |eg4 |
|02 | 89 |eg5 |
|02 | 11 |eg6 |
|02 | 3 |eg7 |
|02 | 25 |eg8 |
我如何获取每个重复的 01 的价格(顺便说一句,它是字符串格式,因为它不是一本书的章节)并将它们加在一起并使它们成为新价格?例如:
Chapter Price
01 57
02 143
The book part isn't really important at the moment so it can be excluded
你试过 DISTINCT 了吗?
SELECT DISTINCT x
from table1
where example = example2
SELECT a.Chapter, sum(a.price)
FROM Table1 a
INNER JOIN Table3 c on c.id = a.id
INNER JOIN Table2 b on b.id = c.id
GROUP BY
a.Chapter
ORDER BY
a.Chapter
你的做法是错误的。你按章节和价格分组,但你也 select 这本书。这本书将是任意选择的章节和价格匹配的书籍之一。所以有了这个数据
book chapter price eg1 01 25 eg1 02 30 eg2 01 25 eg3 01 30
你可能会得到
book chapter price eg1 01 25 eg1 02 30 eg3 01 30
从而抑制图书eg2.
无论如何,GROUP BY chapter
每章都有一个结果行。您得到 SUM
.
select chapter, sum(price)
from chapters
group by chapter;
order by chapter;
只用一个table就可以实现
SELECT
a.Chapter, SUM(a.price) as price
FROM
Table1 a
GROUP BY
a.Chapter
ORDER BY
a.Chapter