请在下面找到我当前的SQL查询。我想要完成的是在结果集中添加一列,每列提供它所属的组的小计。任何人都可以为我提供优雅的解决方案吗?
SELECT costs.cost_des, SUM(costs_periods.actual) AS actual, SUM(costs.commitment) AS commitment, SUM(costs_periods.curr_bud) AS curr_bud,
costs.exp_budget_limit, v_costs_by_group.cost_id, cost_groups.description AS groupDes, cost_groups.cost_group_id,
cost_groups.parent_group_id
FROM costs INNER JOIN
costs_periods ON costs.cost_id = costs_periods.cost_id INNER JOIN
v_costs_by_group ON costs.cost_id = v_costs_by_group.cost_id INNER JOIN
cost_groups ON v_costs_by_group.cost_group_id = cost_groups.cost_group_id
WHERE (costs.year_id =
(SELECT year_id
FROM financial_years
WHERE (year_des = @year))) AND (costs_periods.period <= @currentPeriod) AND (costs_periods.period <> 0) AND (costs_periods.period <> 13)
GROUP BY costs.cost_des, costs.exp_budget_limit, v_costs_by_group.cost_id, cost_groups.cost_group_id, cost_groups.description,
cost_groups.parent_group_id
HAVING (cost_groups.cost_group_id = @CostGroupID5) OR
(cost_groups.parent_group_id = @CostGroupID5)
ORDER BY groupDes, costs.cost_des
非常感谢
安迪
答案 0 :(得分:0)
1您应该在报告工具中执行此操作
2阅读SQL Server帮助文件中的ROLLUP和CUBE运算符
答案 1 :(得分:0)
您需要将WITH ROLLUP放在GROUP BY之后:
GROUP BY
costs.cost_des, costs.exp_budget_limit, v_costs_by_group.cost_id,
cost_groups.cost_group_id, cost_groups.description, cost_groups.parent_group_id
WITH ROLLUP