1

我正在寻找一种更优雅(可能更快)的解决方案来解决我的困境。

我有以下查询来生成关于呼叫流量表的报告:

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

这个查询在每个月底需要更长的时间,我需要减少时间

谁能帮我简化这个?

4

1 回答 1

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
于 2013-03-28T15:00:39.400 回答