如何舍入汇总SQL查询?

时间:2014-11-11 21:46:39

标签: sql aggregate-functions

如何在汇总或汇总函数等方面进行汇总?

ROUND(SUM(T.TICKET_PRICE),2) 

例如:

SELECT EmpId, Yr, SUM(Sales) AS Sales
FROM Sales
GROUP BY EmpId, Yr WITH ROLLUP

3 个答案:

答案 0 :(得分:0)

我能想到的最好的事情是 -

SELECT EmpId, Yr,
       CASE WHEN EmpId IS NULL AND Yr IS NULL
            THEN ROUND(SUM(Sales))
            ELSE SUM(Sales)
       END AS Sales
FROM Sales
GROUP BY EmpId, Yr WITH ROLLUP

不幸的是,如果EmpIdYrNOT NULL,这只会有效。

答案 1 :(得分:0)

将汇总放在子查询中:

SELECT R.EmpID, R.Yr, ROUND(R.Sales,2) AS Sales
FROM (
    SELECT EmpId, Yr, SUM(Sales) AS Sales
    FROM Sales
    GROUP BY EmpId, Yr WITH ROLLUP) R

答案 2 :(得分:0)

你是如此接近,只需用圆函数包装你的总和。

SELECT EmpId, Yr, round(SUM(Sales),2) AS Sales
FROM Sales
GROUP BY EmpId, Yr