2016-05-18 3 views
2

申し訳ありませんが、この問題の適切なタイトルは何ですか。あなたがtable_sumで、すべての手当はすでに「手当」欄に一緒にまとめるされていることを見ることができるようにNULL計算

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

:私は2つのテーブルを持っている

: は、この問題に関する&アドバイスを助けが必要です。私の計画は、手当を控除して計算における引当金の一部が含まれていません - 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 

私はEMPない「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 
+1

何が問題ですか? –

+0

"OLET2-DET1"と "OLET2-DET2"は値を表示するはずですが、Nullを表示します – JackInTheBox10

+0

あなたはKyle Haleの答えと同じようにISNULLを使用できます。 –

答えて

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

ああああ - なぜ私はそれを考えなかった。ありがとう! – JackInTheBox10

1

を私のように、レベルを照会フィールドレベルであることとはならないカイル・ヘイルが、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 
+0

共有いただきありがとうございます。これは私がそれを見ることができるものです。 – JackInTheBox10

+0

「すべきか? ISNULLは、特定のオブジェクトではなく式を評価します。 –

+0

SUMはNULLを無視します。それにもかかわらず、SUM(ISNULL(...))はISNULL(SUM(...))より優先されます。なぜなら、NULLを集約する** ANSI警告**をキャッチしないからです。 –

1

テーブル間の共通フィールドであるfas ter。

ここでは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