尝试将一个SQL查询放在一起,将数据分类到财务年度(我已经排序),然后根据claim_id列中的每个不同值对其进行求和
表格结构是:声明(id,claim_id,日期,金额)
到目前为止的SQL是:
SELECT
CASE
WHEN MONTH(date)>=4 THEN
concat(YEAR(date), '-',YEAR(date)+1)
ELSE
concat(YEAR(date)-1,'-', YEAR(date))
END AS fyear,
SUM(amount) AS total, claim_id
FROM claim
GROUP BY fyear, claim_id
ORDER BY claim_id ASC, date ASC
目前我正在接受这个
Array
(
[fyear] => 2009-2010
[total] => 567.89
[claim_id] => 1
)
哪个非常接近,当然因为GROUP BY claim_id我为每个不同的claim_id获取了另一个数组:
Array
(
[fyear] => 2009-2010
[total] => 106.76
[claim_id] => 2
)
我真正喜欢的是:
Array
(
[fyear] => 2009-2010
[total1] => 567.89
[total2] => 106.76
)
有什么建议吗?
谢谢!
答案 0 :(得分:0)
会是什么......
SELECT * FROM
SELECT (CASE
WHEN MONTH(date)>=4 THEN
concat(YEAR(date), '-',YEAR(date)+1)
ELSE
concat(YEAR(date)-1,'-', YEAR(date))
END AS fyear,
SUM(amount) AS total, claim_id
FROM claim
WHERE claim_id = 1
GROUP BY fyear
ORDER BY date ASC),
SELECT (CASE
WHEN MONTH(date)>=4 THEN
concat(YEAR(date), '-',YEAR(date)+1)
ELSE
concat(YEAR(date)-1,'-', YEAR(date))
END AS fyear,
SUM(amount) AS total, claim_id)
FROM claim
WHERE claim_id = 2
GROUP BY fyear
ORDER BY date ASC)
FROM dual;
有用吗?
答案 1 :(得分:0)
将您现有的查询包装在另一个SELECT
并使用GROUP_CONCAT
可能会完成您的工作。我已经测试了SQL本身,但不确定结果是否符合您的要求 - 所有总计将以逗号分隔列表的形式显示在一列中:
SELECT fyear, GROUP_CONCAT(total) FROM (
SELECT
CASE
WHEN MONTH(date)>=4 THEN
concat(YEAR(date), '-',YEAR(date)+1)
ELSE
concat(YEAR(date)-1,'-', YEAR(date))
END AS fyear,
SUM(amount) AS total, claim_id
FROM table1
GROUP BY fyear, claim_id
ORDER BY claim_id ASC, date ASC
) AS totals GROUP BY fyear;