-1

我在调试我的 SQL 代码时遇到问题,以找出为什么我会产生“遇到除以零错误”。我相信在尝试除以空值并使用 NULLIF 时会发生此错误,据我所知是一种防止这种情况的方法。我的尝试一直不成功,并且可以在我的代码上使用一些额外的眼睛。SQL Server 数据库

SET NOCOUNT ON;
        SELECT  CAST(yr AS INT) AS yr ,
                CAST(rs.RTRIM(D.bus_name) AS VARCHAR(100)) AS bus_name ,
                CAST(rs.RTRIM(D.bus_code) AS VARCHAR(20)) AS bus_code ,
                SUM(CAST(D.salary AS NUMERIC(18, 2))) AS bus_salary , 
                SUM(D.total_assign) AS total_assignment
        FROM    (
        --begin: table:D
                  SELECT    C.bus_code ,
                            C.bus_name ,
                            C.sales_issue_id ,
                            SUM(CAST(C.salary AS NUMERIC(18, 2))) AS salary ,
                            MAX(total_fte) AS total_fte ,
                            SUM(C.assigned_fte) AS total_assign ,
                            MAX(total_salary) AS total_salary ,
                            C.yr
                  FROM      (
                --begin: table C: 
                              SELECT    B.sales_issue_id ,
                                        B.last_name ,
                                        B.firs.t_name ,
                                        B.bus_code ,
                                        B.bus_name ,
                                        B.total_salary ,
                                        B.assigned_fte ,
                                        ( B.total_salary * B.assigned_fte )
                                        / total_fte AS salary ,
                                        B.total_fte ,
                                        B.yr
                              FROM      --table B:
                                        (
                        --begin B: 
                                          SELECT    tb_total_fte_salary.sales_issue_id ,
                                                    rs.bus_name ,
                                                    tb_total_fte_salary.total_fte ,
                                                    rs.region_code
                                                    + rs.county_code
                                                    + rs.bus_code AS bus_code ,
                                                    rs.last_name ,
                                                    rs.first_name ,
                                                    tb_total_fte_salary.total_salary ,
                                                    assigned_fte = CASE rs.assign_fte
                                                              WHEN '' THEN 0
                                                              ELSE CAST(rs.assign_fte AS NUMERIC(18,
                                                              2))
                                                              END ,
                                                    rs.yr
                                          FROM      dbo.raw_sales AS rs, 

                                                    (

                                                      SELECT  sales_issue_id ,
                                                              yr ,
                                                              MAX(last_name) AS last_name ,
                                                              MAX(first_name) AS first_name ,
                                                              SUM(CAST(assign_fte AS NUMERIC(18,
                                                              2))) AS total_fte ,
                                                              MAX(CAST(NULLIF(total_salary,
                                                              0) AS NUMERIC(18,
                                                              2))) AS total_salary
                                                      FROM    raw_sales

                                                      GROUP BY sales_issue_id ,
                                                              yr
                                        --end

                                                    ) tb_total_fte_salary
                                          WHERE     rs.sales_issue_id = tb_total_fte_salary.sales_issue_id
                                                    AND rs.yr = tb_total_fte_salary.yr 

                        --end B:

                                        ) B
                --end C:

                            ) C
                  GROUP BY  C.sales_issue_id ,
                            C.last_name ,
                            first_name ,
                            C.bus_name ,
                            C.bus_code ,
                            C.yr
        --end D:

                ) D
        GROUP BY bus_code ,
                bus_name ,
                yr
        ORDER BY yr ,
                bus_code ,
                bus_name
4

1 回答 1

2

尝试 :

SET NOCOUNT ON;
        SELECT  CAST(yr AS INT) AS yr ,
                CAST(rs.RTRIM(D.bus_name) AS VARCHAR(100)) AS bus_name ,
                CAST(rs.RTRIM(D.bus_code) AS VARCHAR(20)) AS bus_code ,
                SUM(CAST(D.salary AS NUMERIC(18, 2))) AS bus_salary , 
                SUM(D.total_assign) AS total_assignment
        FROM    (
        --begin: table:D
                  SELECT    C.bus_code ,
                            C.bus_name ,
                            C.sales_issue_id ,
                            SUM(CAST(C.salary AS NUMERIC(18, 2))) AS salary ,
                            MAX(total_fte) AS total_fte ,
                            SUM(C.assigned_fte) AS total_assign ,
                            MAX(total_salary) AS total_salary ,
                            C.yr
                  FROM      (
                --begin: table C: 
                              SELECT    B.sales_issue_id ,
                                        B.last_name ,
                                        B.firs.t_name ,
                                        B.bus_code ,
                                        B.bus_name ,
                                        B.total_salary ,
                                        B.assigned_fte ,
                                      -- this is where i changed your query
                                       case when total_fte > 0 then
                                        ( B.total_salary * B.assigned_fte )
                                        / total_fte 
                                          else 0 end
                                         AS salary ,
                                        B.total_fte ,
                                        B.yr
                              FROM      --table B:
                                        (
                        --begin B: 
                                          SELECT    tb_total_fte_salary.sales_issue_id ,
                                                    rs.bus_name ,
                                                    tb_total_fte_salary.total_fte ,
                                                    rs.region_code
                                                    + rs.county_code
                                                    + rs.bus_code AS bus_code ,
                                                    rs.last_name ,
                                                    rs.first_name ,
                                                    tb_total_fte_salary.total_salary ,
                                                    assigned_fte = CASE rs.assign_fte
                                                              WHEN '' THEN 0
                                                              ELSE CAST(rs.assign_fte AS NUMERIC(18,
                                                              2))
                                                              END ,
                                                    rs.yr
                                          FROM      dbo.raw_sales AS rs, 

                                                    (

                                                      SELECT  sales_issue_id ,
                                                              yr ,
                                                              MAX(last_name) AS last_name ,
                                                              MAX(first_name) AS first_name ,
                                                              SUM(CAST(assign_fte AS NUMERIC(18,
                                                              2))) AS total_fte ,
                                                              MAX(CAST(NULLIF(total_salary,
                                                              0) AS NUMERIC(18,
                                                              2))) AS total_salary
                                                      FROM    raw_sales

                                                      GROUP BY sales_issue_id ,
                                                              yr
                                        --end

                                                    ) tb_total_fte_salary
                                          WHERE     rs.sales_issue_id = tb_total_fte_salary.sales_issue_id
                                                    AND rs.yr = tb_total_fte_salary.yr 

                        --end B:

                                        ) B
                --end C:

                            ) C
                  GROUP BY  C.sales_issue_id ,
                            C.last_name ,
                            first_name ,
                            C.bus_name ,
                            C.bus_code ,
                            C.yr
        --end D:

                ) D
        GROUP BY bus_code ,
                bus_name ,
                yr
        ORDER BY yr ,
                bus_code ,
                bus_name
于 2013-04-03T19:15:04.950 回答