我在这里的第一篇文章,希望我遵循发布指南。如果没有,请指出。
我的表结构(credit_notes)包含一些示例数据,如下所示:
index | customer_id | user_id | doc_number | doc_date | amount | scheme_description
+-----+-------------+---------+------------+------------+--------+-------------------
1 | 8 | 2 | IABCD001 | 2012-09-08 | 1232 | XYZ Scheme
2 | 8 | 2 | IABCD001 | 2012-09-08 | 2032 | XYZ Scheme
3 | 8 | 2 | IABCD002 | 2012-09-09 | 2556 | XYZ Scheme
4 | 8 | 2 | IABCD003 | 2012-09-08 | 232 | XYZ Scheme
5 | 8 | 2 | IABCD001 | 2012-09-10 | 2556 | ABC Scheme
6 | 8 | 2 | IABCD003 | 2012-09-10 | 232 | ABC Scheme
我的要求是提取每个scheme_description&的总金额。 doc_date组合,分组。这是我目前的查询:
SELECT
SUM( `amount` ) AS `totalamount`,
GROUP_CONCAT( CONCAT( `amount` , ";" ) ORDER BY `index` DESC SEPARATOR '' ) AS `amounts`,
GROUP_CONCAT( CONCAT( `doc_number` , ";" ) ORDER BY `doc_number` DESC SEPARATOR '' ) AS `docno` ,
`doc_date` AS `docdate` ,
`scheme_description` AS `schemedesc`
FROM (
SELECT
`c`.`index` ,
`c`.`doc_number` ,
`c`.`doc_date` ,
`c`.`amount` ,
`c`.`scheme_description`
FROM `credit_notes` AS `c`
WHERE `c`.`customer_id` = 8
AND `c`.`user_id` LIKE 2
)t
GROUP BY `scheme_description` , `doc_date`
此查询返回此结构:
totalamount | amounts | docno | docdate | schemedesc
+-----------+----------------+-----------------------------+------------+-----------
3496 | 1232;2032;232; | IABCD001;IABCD001;IABCD003; | 2012-09-08 | XYZ Scheme
2556 | 2556; | IABCD002; | 2012-09-09 | XYZ Scheme
23477 | 255;23222; | IABCD007;IABCD007; | 2012-09-10 | ABC Scheme
但是,我需要稍微更改一下,我想为每个不同的 docno 汇总金额列,并按正确的顺序对其进行分组。因此,根据上述数据,查询应该产生:
totalamount | amounts | docno | docdate | schemedesc
+-----------+----------------+-----------------------------+------------+-----------
3496 | 3264;232; | IABCD001;IABCD003; | 2012-09-08 | XYZ Scheme
2556 | 2556; | IABCD002; | 2012-09-09 | XYZ Scheme
23477 | 23477; | IABCD007; | 2012-09-10 | ABC Scheme
这是否可以使用group_concat和distinct和sum。阅读我能找到的所有内容,但找不到任何指向正确方向的内容。
这可能吗?
答案 0 :(得分:1)
您的第一个查询可以写成:
select
doc_date, scheme_description,
group_concat(doc_number),
group_concat(amount),
sum(amount)
from
credit_notes
group by doc_date, scheme_description
和你的第二个:
select
doc_date, scheme_description,
group_concat(doc_number),
group_concat(amount),
sum(amount)
from (
select doc_date, scheme_description, doc_number, sum(amount) as amount
from credit_notes
group by doc_date, scheme_description, doc_number
) s
group by doc_date, scheme_description
答案 1 :(得分:0)
尝试此查询 -
SELECT
SUM(amount) totalamount,
GROUP_CONCAT(amount) amounts,
GROUP_CONCAT(doc_number) docno,
doc_date docdate,
scheme_description schemedesc
FROM (
SELECT scheme_description, doc_date, doc_number, SUM(amount) amount
FROM credit_notes
GROUP BY scheme_description, doc_date, doc_number) t
GROUP BY scheme_description, doc_date
如果需要,添加WHERE,ORDER BY和其他子句。
答案 2 :(得分:0)
尝试此查询
select
group_concat(doc_num) as DocNumm,
group_concat(amount) as DocNumm,
scheme_description as SchemaDesc,
sum(amount) as TotalAmount,
amount as Amount,
doc_date as DocDate
from (
SELECT * from `schema`
) as g
group by SchemaDesc , DocDate