我正在尝试一个简单的查询,试图显示12个时期的总数。我正在使用SUM(SUM(value))
函数来检索我想要的数据,但是,我很难在结果中显示第二列。
SELECT CENTRE, SUM(SUM(AMOUNT)) "TOTAL PAY" FROM AB
WHERE ACCOUNT LIKE 'N%' AND CENTRE = '2001' AND YEAR > 2015 GROUP BY AMOUNT, CENTRE;
我得到的错误与句子的分组有关。
你能告诉我我做错了吗?我用子查询解决了这个问题,但我也需要修复这个查询,因为它在更高级的查询中用作子查询。答案 0 :(得分:0)
你的问题太模糊了,无法确定你想要什么。例如,你的意思是"总共12个时期"?对YEAR > 2015
你有12行吗?你的CENTRE =
条款中总是有WHERE
吗?如果是这样,这可能就是你想要的:
SELECT
MAX(centre) "CENTRE",
SUM(amount) "TOTAL PAY"
FROM
ab
WHERE
account LIKE 'N%'
AND
centre = '2001'
AND
year > 2015;
如果CENTRE = 'smth'
条款中WHERE
可能不在您的SELECT
centre "CENTRE",
SUM(amount) "TOTAL PAY"
FROM
ab
WHERE
account LIKE 'N%'
/* AND
centre = '2001'*/
AND
year > 2015
GROUP BY
centre;
条款中,您需要每个中心的总价值:
SELECT
"CENTRE",
total "TOTAL PAY"
FROM
(
SELECT
centre,
ROW_NUMBER() OVER(PARTITION BY
centre
ORDER BY
0
) rn,
SUM(
amount
) OVER(PARTITION BY
0
) total
FROM
ab
WHERE
account LIKE 'N%'
AND
year > 2015
)
WHERE
rn = 1;
或者,对于每个(或一个)CENTER行,您需要具有所有中心的总值:
SET @Date = '20170727'; -- Format as YYYYMMDD which is locale neutral