此查询太重,需要重构。我该怎么办?

问题描述 投票:0回答:1

此查询太重,需要重构。我能怎么做?请帮助

SELECT contract_type , SUM(fte),round(SUM(fte * 100 / t.s ),0) AS "% of total"
FROM design_studio_testing.empfinal_tableau 
CROSS JOIN (SELECT SUM(fte) AS s FROM design_studio_testing.empfinal_tableau) t
group by contract_type;

输出应该是这样。

enter image description here

sql sql-server
1个回答
0
投票

不需要对表达式求和:

fte * 100 / t.s 

可能会减慢处理速度。得到SUM(fte),然后相乘和相除:

SELECT 
  contract_type, 
  SUM(fte),
  ROUND(100.0 * SUM(fte) / t.s, 0) AS [% of total]
FROM design_studio_testing.empfinal_tableau 
CROSS JOIN (SELECT SUM(fte) AS s FROM design_studio_testing.empfinal_tableau) t
GROUP BY contract_type;
© www.soinside.com 2019 - 2024. All rights reserved.