我有一个包含以下列的表。
我想获得具有相同列数的结果,但是运行余额与帐户代码的总和不会重复多次 我试过这个查询
select AccountCode , 'All Companies' as divisionName , AcName , LF_AccountType , Sum(ISNULL(runningBalance,0)) as runningBalance from Table
group by AccountCode , divisionName , AcName , LF_AccountType
答案 0 :(得分:2)
目前还不清楚你的问题是什么。但是,group by语句中的所有列必须位于查询的表中或从中查询。所以你不能在你的组中使用divisionname。你不需要它。 所以,立即的句法修复将是:
<div class="">
<input id="toggle" type="checkbox" value=""><label for="toggle" class ="visible">Add to compare</label>
</div>
答案 1 :(得分:0)
因为您使用的是固定值,只需使用聚合函数并将其从GROUP BY
子句中删除,就没有意义了。例如:
SELECT AccountCode , AcName , LF_AccountType ,
MIN('All Companies') as divisionName,
SUM(ISNULL(runningBalance,0)) as runningBalance from Table
group by AccountCode , AcName , LF_AccountType
使用带有子查询的Windows函数进行TRY:
SELECT * FROM (
Select AccountCode , 'All Companies' as divisionName , AcName , LF_AccountType ,
Sum(ISNULL(runningBalance,0)) OVER (PARTITION BY LF_AccountType ) as runningBalance ,
ROW_NUMBER() OVER(PARTITION BY LF_AccountType) as rn
from Table
) AS T1 WHERE T1.rn = 1
或使用Group by
的子查询Select 'All Companies' as divisionName, * From (
select AccountCode , AcName ,
LF_AccountType , Sum(ISNULL(runningBalance,0)) as runningBalance
from Table
group by AccountCode, AcName , LF_AccountType
) T1