在项目中发现有这样的写法
SELECT ZoneID,CountSQAZFZSBJZ3G+CountSQGZJRJZSL3G AS column1
FROM
G3MulticarrierSiteCoverTableFrm
union
SELECT 合计 as ZoneID,sum(CountSQAZFZSBJZ3G+CountSQGZJRJZSL3G) AS
column1
FROM G3MulticarrierSiteCoverTableFrm
发面“CountSQAZFZSBJZ3G+CountSQGZJRJZSL3G”有很多这样的业务,都是复制一次,容易漏掉,导致数据不对。
可以改成
在这里写一次“CountSQAZFZSBJZ3G+CountSQGZJRJZSL3G”就行了。
select isnull(CONVERT(varchar(20), ZoneID),‘Total‘) as ‘emp_id‘
,sum(t.column1) as ‘qty_Total‘
from (SELECT
ZoneID,CountSQAZFZSBJZ3G+CountSQGZJRJZSL3G AS column1
FROM
G3MulticarrierSiteCoverTableFrm) t
group by ZoneID
with rollup