SQl计算百分比以避免存储过程中出现Div / 0错误

如何解决SQl计算百分比以避免存储过程中出现Div / 0错误

我正在尝试更新一些旧的存储过程,这些存储过程目前尚无法解决

它从多个表中提取数据以为基于表的报告创建记录集。

我遇到的问题是利用率数字,这些是百分比,但它们经常会出现除以零的误差。

这是当前脚本。

CREATE PROC [PPA].[MonthlyUtilisation]

--EXEC [PPA].[MonthlyUtilisationIndividual] @Year = 1920,@Employee = 7238734,@Month = 5,@Area = 'National',@Team = 'SET'

@Year int,@Employee int,@Month int,@Area varchar(55),@Team varchar(55)

AS

DECLARE @MonthID int = (SELECT disTINCT CONCAT([ReportYear],[ReportMonth]) FROM [PPA].[Weeks] WHERE [ReportYear] = @Year AND [ReportMonth] = @Month)

SELECT 
Ep.[EmployeePID] AS [Staff PID],E.[FirstName] AS [First Name],E.[LastName] AS [Last Name],G.[GradeName] AS [Staff Grade],Tms.[TeamName] As [Team],Count(Ep.[EmployeePID]) AS [*],Sum([WeeklyContractedHours]) AS [GROSS Available Resource],Isnull(Sum(f.mins),0) AS [Planned Flexi],Isnull(Sum(af.mins),0) AS [Actual Flexi],Isnull(Sum(l.mins),0) AS [Planned Leave],Isnull(Sum(al.mins),0) AS [Actual Leave],Sum([WeeklyContractedHours] 
+ Isnull(f.mins,0) 
- Isnull(l.mins,0))
AS [Planned NET Available Resource],Sum([WeeklyContractedHours] 
+ Isnull(af.mins,0) 
- Isnull(al.mins,0))
AS [Actual NET Available Resource],Isnull(Sum(i.mins),0) AS [Planned Indirects],Isnull(Sum(ai.mins),0) AS [Actual Indirects],Sum([WeeklyContractedHours]
+ Isnull(f.mins,0)
- Isnull(l.mins,0))
- Isnull(Sum(i.mins),0)
AS [Planned GROSS Direct Resource],Sum([WeeklyContractedHours]
+ Isnull(af.mins,0)
- Isnull(al.mins,0))
- Isnull(Sum(ai.mins),0)
AS [Actual GROSS Direct Resource],Round(Cast(Sum([WeeklyContractedHours]
+ Isnull(f.mins,0)) 
- Isnull(Sum(i.mins),0) AS FLOAT) 
/ Cast(Sum([WeeklyContractedHours] 
+ Isnull(f.mins,0)) AS FLOAT) 
* 100,0)
AS [Planned Utilisation],Round(Cast(Sum([WeeklyContractedHours] 
+ Isnull(af.mins,0)) 
- Isnull(Sum(ai.mins),0) AS FLOAT) 
/ Cast(Sum([WeeklyContractedHours] 
+ Isnull(af.mins,0)
AS [Actual Utilisation]
  
FROM [PPA].[EmployeeProfiles] AS Ep 
/*=====================================
======================================*/

LEFT JOIN [PPA].[Timesheets] as Ts on Ts.[EmployeeProfileID] = Ep.[EmployeeProfileID]
LEFT JOIN [PPA].[employees] AS E ON Ep.[EmployeePID] = E.[EmployeePID] 
JOIN [PPA].[grades] AS G ON Ep.[GradeID] = G.[GradeID]
JOIN [PPA].[teams] as Tms ON Ep.[TeamID] = Tms.[TeamID]
JOIN [PPA].[areas] as A ON Tms.[AreaID] = A.[AreaID]
JOIN [PPA].[Weeks] as Wk on Ts.[WeekID] = wk.[WeekID]

--Planned Flexi Leave
LEFT JOIN (SELECT T.[TimesheetID],Isnull(Sum(mins),0) AS Mins 
FROM [PPA].[Stage1Leave] AS S1L 
JOIN [PPA].[timesheets] AS T ON S1L.[TimesheetID] = T.[TimesheetID]
JOIN [PPA].[Weeks] as Wk ON T.[WeekID] = wk.[WeekID]
WHERE  LeaveID = 10
AND Wk.[ReportYear]= @Year and Wk.[ReportMonth] = @Month
GROUP  BY T.[TimesheetID]) AS f 
ON f.[TimesheetID] = Ts.[TimesheetID]

--Actual Flexi Leave
LEFT JOIN (SELECT T.[TimesheetID],0) AS Mins 
FROM [PPA].[Stage3Leave] AS S3L 
JOIN [PPA].[timesheets] AS T ON S3L.[TimesheetID] = T.[TimesheetID]
JOIN [PPA].[Weeks] as Wk ON T.[WeekID] = wk.[WeekID]
WHERE  LeaveID = 10
AND Wk.[ReportYear]= @Year and Wk.[ReportMonth] = @Month
GROUP  BY T.[TimesheetID]) AS af 
ON af.[TimesheetID] = Ts.[TimesheetID]

--Planned Leave
LEFT JOIN (SELECT T.[TimesheetID],0) AS Mins 
FROM  [PPA].[Stage1Leave] AS S1L
JOIN [PPA].[timesheets] AS T ON S1L.[TimesheetID] = T.[TimesheetID]
JOIN [PPA].[Weeks] as Wk on T.[WeekID] = wk.[WeekID]
WHERE  LeaveID NOT IN (10,12)
AND Wk.[ReportYear]= @Year and Wk.[ReportMonth] = @Month 
GROUP  BY T.[TimesheetID]) AS l 
ON l.[TimesheetID] = Ts.[TimesheetID] 

--Actual Leave
LEFT JOIN (SELECT T.[TimesheetID],0) AS Mins 
FROM   [PPA].[Stage3Leave] AS S3L 
JOIN [PPA].[timesheets] AS T ON S3L.[TimesheetID] = T.[TimesheetID]
JOIN [PPA].[Weeks] as Wk on T.[WeekID] = wk.[WeekID]
WHERE  LeaveID <> 10
AND Wk.[ReportYear]= @Year and Wk.[ReportMonth] = @Month
GROUP  BY T.[TimesheetID]) AS al 
ON al.[TimesheetID] = Ts.[TimesheetID] 

--Planned Indirects
LEFT JOIN (SELECT T.[TimesheetID],0) AS Mins 
FROM   [PPA].[stage1] AS S 
JOIN [PPA].[timesheets] AS T ON S.[TimesheetID] = T.[TimesheetID]
JOIN [PPA].[Weeks] as Wk on T.[WeekID] = wk.[WeekID]
WHERE Wk.[ReportYear]= @Year and Wk.[ReportMonth] = @Month
GROUP  BY T.[TimesheetID]) AS i 
ON i.[TimesheetID] = Ts.[TimesheetID]

--Actual Indirects
LEFT JOIN (SELECT T.[TimesheetID],0) AS Mins 
FROM   [PPA].[stage3indirects] AS s3i
JOIN [PPA].[timesheets] AS T ON s3i.[TimesheetID] = T.[TimesheetID]
JOIN [PPA].[Weeks] as Wk on T.[WeekID] = wk.[WeekID]
WHERE Wk.[ReportYear]= @Year and Wk.[ReportMonth] = @Month
GROUP  BY T.[TimesheetID]) AS ai 
ON ai.[TimesheetID] = Ts.[TimesheetID]

WHERE Ts.[EmployeePID] = CASE WHEN @Employee = 0 THEN Ts.[EmployeePID] ELSE @Employee END
AND Tms.[TeamName] = CASE WHEN @Team = 'All' THEN [TeamName] ELSE @Team END
--AND (SELECT CONCAT([ReportYear],[ReportMonth])) = @MonthID
AND Wk.[ReportYear]= @Year and Wk.[ReportMonth] = @Month
and Ep.[Live] = 1
GROUP  BY Tms.[TeamName],G.[GradeName],G.[GradeID],E.[LastName],E.[FirstName],Ep.[EmployeePID]
ORDER  BY Tms.[TeamName],G.[GradeID]

GO

这是我苦苦挣扎的脚本的一部分

,0)
AS [Actual Utilisation]

任何帮助将不胜感激。

解决方法

我认为最简单的方法是用<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="@color/colorAccent" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </com.google.android.material.appbar.AppBarLayout> <TextView android:id="@+id/words_prompt" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" android:layout_below="@+id/appbar" android:layout_margin="10dp" android:text="@string/words_prompt" /> <EditText android:id="@+id/word_list" android:singleLine="false" android:enabled="false" android:lines="10" android:textStyle="bold" android:textColor="@color/colorPrimaryDark" android:background="#ffffff" android:inputType="textMultiLine" android:layout_below="@+id/words_prompt" android:layout_margin="10dp" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" android:gravity="top" android:layout_width="180sp" android:layout_height="wrap_content"/> <TextView android:id="@+id/time_left_prompt" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/word_list" android:layout_toEndOf="@+id/word_list" android:layout_below="@+id/words_prompt" android:layout_margin="10dp" android:text="@string/time_left_prompt" /> <TextView android:id="@+id/time_left" android:textStyle="bold" android:layout_below="@+id/words_prompt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/time_left_prompt" android:layout_toEndOf="@+id/time_left_prompt" android:layout_margin="10dp" /> <EditText android:id="@+id/typed_word" android:focusable="false" android:maxLength="10" android:inputType="text" android:textCursorDrawable="@drawable/custom_cursor" android:enabled="false" android:layout_below="@+id/word_list" android:digits="abcdefghijklmnopqrstuvwxyz" android:textColor="@color/colorPrimaryDark" android:layout_margin="10dp" android:hint="@string/type_hint" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/done_button" android:enabled="false" android:text="@string/done_prompt" android:layout_toRightOf="@+id/typed_word" android:layout_toEndOf="@+id/typed_word" android:layout_alignBaseline="@+id/typed_word" android:layout_below="@+id/word_list" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <include layout="@layout/keyboardlayout_en" android:id="@+id/keyboard_layout" android:visibility="invisible" android:layout_centerInParent="true" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/done_button"/> <Button android:id="@+id/start_timer_button" android:text="@string/start_timer_prompt" android:layout_margin="10dp" android:layout_below="@+id/done_button" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/best_score_prompt" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/start_timer_button" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:text="@string/best_score_prompt" /> <TextView android:id="@+id/best_score" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/start_timer_button" android:layout_toRightOf="@+id/best_score_prompt" android:layout_toEndOf="@+id/best_score_prompt" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:layout_marginRight="10dp" android:layout_marginEnd="10dp" /> <TextView android:id="@+id/elapsed_time_prompt" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="invisible" android:layout_below="@+id/best_score_prompt" android:layout_margin="10dp" android:text="@string/elapsed_time_prompt" /> <TextView android:id="@+id/elapsed_time" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="invisible" android:layout_below="@+id/best_score_prompt" android:layout_toRightOf="@+id/elapsed_time_prompt" android:layout_toEndOf="@+id/elapsed_time_prompt" android:layout_margin="10dp" /> <Button android:id="@+id/reset_button" android:text="@string/reset_prompt" android:enabled="false" android:layout_alignParentBottom="true" android:layout_below="@+id/best_score_prompt" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/quit_button" android:text="@string/quit_prompt" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_below="@+id/best_score_prompt" android:layout_marginRight="10dp" android:layout_marginEnd="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> 包装除数,这样,如果值具有NULLIF,则该值变为NULL

例如:

0

附带说明,在编写SQL时请考虑使用使用空格。该代码很难阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?