如何在这样的 GROUP BY 子句中使用聚合函数?
SELECT VCH_DCOA_CODE, SUM(VCH_DEFINATION.VCH_CREDIT) AS Debit
FROM VCH_DEFINATION
GROUP BY SUBSTRING(VCH_DCOA_CODE, 0, 6)
如何在这样的 GROUP BY 子句中使用聚合函数?
SELECT VCH_DCOA_CODE, SUM(VCH_DEFINATION.VCH_CREDIT) AS Debit
FROM VCH_DEFINATION
GROUP BY SUBSTRING(VCH_DCOA_CODE, 0, 6)
看起来你想显示sum()
完整VCH_DCOA_CODE
但group by
子字符串值,所以我猜你想要这个:
SELECT v1.VCH_DCOA_CODE, v2.Debit
FROM VCH_DEFINATION v1
INNER JOIN
(
select SUM(VCH_DEFINATION.VCH_CREDIT) AS Debit, SUBSTRING(VCH_DCOA_CODE, 0, 6) Shortcode
from VCH_DEFINATION
group by SUBSTRING(VCH_DCOA_CODE, 0, 6)
) v2
on v1.SUBSTRING(VCH_DCOA_CODE, 0, 6) = v2.Shortcode
子查询将获得SUM()
for eachSUBSTRING(VCH_DCOA_CODE, 0, 6)
但随后将显示每个完整的VCH_DCOA_CODE
但是,如果您不想显示完整的VCH_DCOA_CODE
,那么您可以使用内部查询来获取结果:
select SUM(VCH_DEFINATION.VCH_CREDIT) AS Debit, SUBSTRING(VCH_DCOA_CODE, 0, 6) Shortcode
from VCH_DEFINATION
group by SUBSTRING(VCH_DCOA_CODE, 0, 6)
您必须添加SUBSTRING(VCH_DCOA_CODE, 0, 6)
如下select
条款:
SELECT SUBSTRING(VCH_DCOA_CODE, 0, 6), SUM(VCH_DEFINATION.VCH_CREDIT) AS Debit
FROM VCH_DEFINATION
GROUP BY SUBSTRING(VCH_DCOA_CODE, 0, 6)