NULL计算

时间:2016-05-18 05:33:11

标签: sql-server sql-server-2008 sum

抱歉,不确定此问题的正确标题是什么。 需要帮助&关于这个问题的建议:

我有两张桌子:

--table_sum   
SELECT Outlet + '-' + Department as Outlet, 
COUNT (*) as Headcount, 
SUM (Basic + Allowance + Overtime) - Deduction as Amount 
FROM table_sum WHERE 
year = 2016 AND month = 4 AND  
(Basic + Allowance + Overtime)- Deduction > 0 
GROUP BY Outlet, Department

Emp no  Outlet  Department   Basic     Allowance    Overtime    Deduction
 101     OLET1    DET1     $2,000.00    $250.00      $30.00      $10.00 
 102     OLET2    DET2     $1,800.00    $100.00      $50.00      $10.00 
 103     OLET1    DET1     $2,500.00    $250.00      $20.00      $-   
 104     OLET2    DET1     $3,500.00    $100.00      $-           $-   


--table_details
SELECT SUM (Amount) 
FROM Table_details 
WHERE year = 2016 AND month = 4 
AND Code  = 'OTA'

Emp No  Outlet  Department  Code    Code Description      Amount
 101    OLET1   DET1        BSC     Basic                $2,000.00 
 101    OLET1   DET1        CRA     Car Allowance        $100.00 
 101    OLET1   DET1        OTA     Other Allowance      $150.00 
 101    OLET1   DET1        OTP     Normal Day Overtime  $30.00 
 101    OLET1   DET1        UFD     Uniform Deduction    $10.00 
 102    OLET2   DET2        BSC     Basic                $1,800.00 
 102    OLET2   DET2        CRA     Car Allowance        $100.00 
 102    OLET2   DET2        OTP     Normal Day Overtime  $50.00 
 102    OLET2   DET2        UFD     Uniform Deduction    $10.00 
 103    OLET1   DET1        BSC     Basic                $2,500.00 
 103    OLET1   DET1        CRA     Car Allowance        $100.00 
 103    OLET1   DET1        OTA     Other Allowance      $150.00 
 103    OLET1   DET1        OTP     Normal Day Overtime  $20.00 
 104    OLET2   DET1        BSC     Basic                $3,500.00 
 104    OLET2   DET1        CRA     Car Allowance        $100.00 

正如你在table_sum中看到的那样,所有的免税额已经在" Allowance"中得到了总结。柱。我的计划不包括扣除津贴的计算中的一些津贴 - 这就是table_details的来源:

SELECT Outlet + '-' + Department as Outlet, 
COUNT (*) as Headcount, 
SUM (Basic + Allowance + Overtime) - Deduction - 
   (SELECT SUM (z.Amount) FROM Table_details z 
    WHERE z.Outlet=Table_sum.Outlet AND z.Outlet=Table_sum 
    AND z.Department=Table_sum.Department 
    AND z.year = 2016 AND z.month = 4 
    AND z.Code  = 'OTA') as  Amount --deduct OTA Allowance Amount
FROM table_sum 
WHERE year = 2016 AND month = 4 
AND  (Basic + Allowance + Overtime)- Deduction > 0 
GROUP BY Outlet, Department

结果:

  Outlet               Headcount         Amount
OLET1-DET1                 2             $4,740
OLET2-DET1                 1              NULL
OLET2-DET2                 1              NULL

我意识到在#34; OLET2-DET1" &安培; " OLET2-DET2"没有' OTA'津贴。因此,结果变为NULL。对此有何建议?

谢谢!

解决:

SELECT Outlet + '-' + Department as Outlet, 
COUNT (*) as Headcount, 
SUM (Basic + Allowance + Overtime) - Deduction - 
   ISNULL((SELECT SUM (z.Amount) FROM Table_details z 
    WHERE z.Outlet=Table_sum.Outlet AND z.Outlet=Table_sum 
    AND z.Department=Table_sum.Department 
    AND z.year = 2016 AND z.month = 4 
    AND z.Code  = 'OTA'),0) as  Amount --deduct OTA Allowance Amount
FROM table_sum 
WHERE year = 2016 AND month = 4 
AND  (Basic + Allowance + Overtime)- Deduction > 0 
GROUP BY Outlet, Department

3 个答案:

答案 0 :(得分:1)

将子查询包装在ISNULL语句中:

SELECT Outlet + '-' + Department as Outlet, 
COUNT (*) as Headcount, 
SUM (Basic + Allowance + Overtime) - Deduction - 
   ISNULL ( (SELECT SUM (z.Amount) FROM Table_details z 
    WHERE z.Outlet=Table_sum.Outlet AND z.Outlet=Table_sum 
    AND z.Department=Table_sum.Department 
    AND z.year = 2016 AND z.month = 4 
    AND z.Code  = 'OTA'), 0 ) as  Amount --deduct OTA Allowance Amount
FROM table_sum 
WHERE year = 2016 AND month = 4 
AND  (Basic + Allowance + Overtime)- Deduction > 0 
GROUP BY Outlet, Department

答案 1 :(得分:1)

我尊重Kyle Hale给出的答案,但ISNULL应该是字段级而不是查询级别,如下所示

SELECT Outlet + '-' + Department as Outlet, 
COUNT (*) as Headcount, 
SUM (Basic + Allowance + Overtime) - Deduction - 
   (SELECT SUM (ISNULL (z.Amount, 0)) FROM Table_details z 
    WHERE z.Outlet=Table_sum.Outlet AND z.Outlet=Table_sum 
    AND z.Department=Table_sum.Department 
    AND z.year = 2016 AND z.month = 4 
    AND z.Code  = 'OTA') as  Amount --deduct OTA Allowance Amount
FROM table_sum 
WHERE year = 2016 AND month = 4 
AND  (Basic + Allowance + Overtime)- Deduction > 0 
GROUP BY Outlet, Department

答案 2 :(得分:1)

当表之间的公共字段更快时,您必须始终使用连接查询。

这是2方法,也阅读评论中的说明。

--Approach 1 : simple join
SELECT Outlet + '-' + Department as Outlet, 
COUNT (*) as Headcount, 
SUM (Basic + Allowance + Overtime) - Deduction - 
ISNULL ( SUM (z.Amount)   as  Amount --deduct OTA Allowance Amount

FROM table_sum ts  --give alias name to identiy easily
--add left outer, so if null value found, then main table data is not mismatch
Left outer JOIN Table_details z on z.Outlet=Table_sum.Outlet and z.year = ts.year and z.month = ts.month and z.code = 'OTA' 
WHERE ts.year = 2016 AND month = 4 
AND  (ts.Basic + ts.Allowance + ts.Overtime)- ts.Deduction > 0 
GROUP BY ts.Outlet, ts.Department


--Approach 2 : Use CTE and join
;with cte
as
(
    SELECT z.Outlet, SUM (z.Amount) 
    FROM Table_details z 
    WHERE z.year = 2016 AND z.month = 4 
        AND z.Code  = 'OTA'
    Group By z.Outlet
)

SELECT Outlet + '-' + Department as Outlet, 
COUNT (*) as Headcount, 
SUM (Basic + Allowance + Overtime) - Deduction - 
ISNULL ( SUM (z.Amount)   as  Amount --deduct OTA Allowance Amount

FROM table_sum ts  --give alias name to identiy easily
Left outer JOIN cte z on z.Outlet=Table_sum.Outlet --add left outer, so if null value found, then main table data is not mismatch
WHERE ts.year = 2016 AND month = 4 
AND  (ts.Basic + ts.Allowance + ts.Overtime)- ts.Deduction > 0 
GROUP BY ts.Outlet, ts.Department