2013-04-03 57 views
-1

我在調試我的SQL代碼時發現了一個問題,以瞭解爲什麼我要生成「除以零的錯誤」。我相信這個錯誤發生在嘗試除以空值並使用NULLIF時,據我所知是一種防止這種情況的方法。我在嘗試中沒有成功,並且可能會在代碼上使用一些額外的眼睛。 SQL Server數據庫SQL除以零所遇到的錯誤

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 
+0

我只看到一個地方,你正在做的分裂,使你想從那裏開始。 – jbabey

回答

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