我正在寻找一种更优雅(可能更快)的解决方案来解决我的困境。
我有以下查询来生成呼叫流量表的报告:
SELECT 'Tariff1' as [Tariff], Count([Number]) as [Number of calls] ,sum(convert(bigint,[Seconds]))/60 as [Minutes], sum([CustomerCost]) as [Customer Cost], sum([WholesaleCost]) as [WholesaleCost]
FROM [MarchCalls]
where [AccNo] in (select [accno] from [Tariffs] where [Tariff] = 'Tariff1')
union
SELECT 'Tariff2' as [Tariff], Count([Number]) as [Number of calls] ,sum(convert(bigint,[Seconds]))/60 as [Minutes], sum([CustomerCost]) as [Customer Cost], sum([WholesaleCost]) as [WholesaleCost]
FROM [MarchCalls]
where [AccNo] in (select [accno] from [Tariffs] where [Tariff] = 'Tariff2')
union
SELECT 'Tariff3' as [Tariff], Count([Number]) as [Number of calls] ,sum(convert(bigint,[Seconds]))/60 as [Minutes], sum([CustomerCost]) as [Customer Cost], sum([WholesaleCost]) as [WholesaleCost]
FROM [MarchCalls]
where [AccNo] in (select [accno] from [Tariffs] where [Tariff] = 'Tariff3')
union
SELECT 'Total' as [Tariff], Count([Number]) as [Number of calls] ,sum(convert(bigint,[Seconds]))/60 as [Minutes], sum([CustomerCost]) as [Customer Cost], sum([WholesaleCost]) as [WholesaleCost]
FROM [MarchCalls]
有几点: 由于记录量和其中的数字,需要转换(bigint) 返回的记录集必须保持此格式 此数据(和数据库)严格只读 SQL Server 2012
这个查询在每个月末需要更长的时间,我需要把时间缩短
有人可以帮我简化这个吗?
答案 0 :(得分:4)
您可以加入AccNo字段,而不是使用IN:
SELECT
T.[Tariff],
Count([Number]) as [Number of calls] ,
sum(convert(bigint, [Seconds]))/60 as [Minutes],
sum([CustomerCost]) as [Customer Cost],
sum([WholesaleCost]) as [WholesaleCost]
FROM [MarchCalls] M
inner join [Tarrifs] T on M.AccNo=T.Accno and [Tariff] in ('Tariff1','Tariff2', 'Tariff3')
group by [Tariff] with ROLLUP