申し訳ありませんが、この問題の適切なタイトルは何ですか。あなたが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
何が問題ですか? –
"OLET2-DET1"と "OLET2-DET2"は値を表示するはずですが、Nullを表示します – JackInTheBox10
あなたはKyle Haleの答えと同じようにISNULLを使用できます。 –