[英]How do I get the SUM of a group in an sql query
My query is 我的查询是
SELECT *
FROM acodes
WHERE datenumber >= '2016-12-09'
GROUP BY campaignid, acode
LIMIT 0 , 30
Results are 结果是
Is there a way to SUM() the maxworth
column? 有没有一种方法可以对
maxworth
列进行SUM()? I want to add up all the maxworth
shown above in an sql query. 我想将上面显示的所有
maxworth
都添加到sql查询中。 The answer is not SELECT *, SUM(maxworth)
as there are multiple maxworth
for the same acode
and campaignid
. 答案不是
SELECT *, SUM(maxworth)
因为相同的acode
和campaignid
有多个maxworth
。
Not sure what you're asking here. 不知道您在这里问什么。
SELECT
a.MAXWORTH1,
SUM(a.MAXWORTH) AS "MAXWORTH2"
FROM (
SELECT
CAMPAIGNID,
SUM(maxworth) AS "MAXWORTH1"
FROM acodes
WHERE datenumber ='2016-12-05'
GROUP BY campaignid
) a
GROUP BY a.MAXWORTH1
Reference the existing query as an inline view. 引用现有查询作为内联视图。 Take the existing query, and wrap in parens, and then use that in place of a table name in another query.
使用现有查询,并包装括号,然后在另一个查询中使用该查询代替表名。 (The inline view will need to be assigned an alias.)
(内联视图将需要分配一个别名。)
For example: 例如:
SELECT SUM(v.maxworth)
FROM (
-- existing query goes here, between the parens
SELECT *
FROM acodes
WHERE datenumber >= '2016-12-09'
GROUP BY campaignid, acode
LIMIT 0 , 30
) v
In MySQL, that inline view is referred to as a derived table . 在MySQL中,该内联视图称为派生表 。 The way that query works.... MySQL first runs the query in the inline view, and materializes the resultset into a derived table.
查询的工作方式。...MySQL首先在嵌入式视图中运行查询,并将结果集具体化为派生表。 Once the derived table is populated, the outer query runs against that.
填充派生表后,外部查询将针对此表运行。
This calculates the SUM() by unique campaignid, acode, and maxworth. 这将通过唯一的campaignid,acode和maxworth计算SUM()。 You mention "there are multiple maxworth for the same acode and campaignid" which makes me think you might be wanting to treat "maxworth" as unique.
您提到“同一代码和campaignid有多个maxworth”,这使我认为您可能想将“ maxworth”视为唯一。
SELECT
campaignid,
acode,
maxworth,
SUM(maxworth) AS 'MAXWORTH1'
FROM acodes
WHERE datenumber >= '2016-12-09'
GROUP BY campaignid, acode, maxworth
Here is another attempt at answering your question. 这是回答您问题的另一种尝试。
SELECT
a.campaignid,
a.acode,
SUM(a.maxworth) as "SUMMAXWORTH"
FROM
(SELECT
*
FROM acodes
WHERE datenumber >= '2016-12-09'
GROUP BY campaignid, acode
LIMIT 0 , 30
) a
GROUP BY a.campaignid, a.acodes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.