Sql server 零计算
抱歉,不确定此问题的正确标题是什么。 需要有关此问题的帮助和建议: 我有两张桌子:Sql server 零计算,sql-server,sql-server-2008,sum,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 + Al
--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
正如您可以看到的,在表_sum中,所有的备抵都已在“备抵”列中汇总。我的计划不包括通过扣除津贴计算的部分津贴-这是表_详细信息的来源:
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
我意识到“OLET2-DET1”和“OLET2-DET2”中的emp no没有“OTA”余量。所以,结果变为空。有什么建议吗
谢谢
已解决:
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
只需将子查询包装在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
我尊重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
当在表之间创建公共字段时,必须始终使用联接查询,这会更快 这里有两种方法,也请阅读注释中的说明
--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
问题是什么?OLET2-DET1和OLET2-DET2应该显示值,但它显示为空。您可以使用ISNULL,就像Kyle Hale的答案一样。谢谢-我没有想到它。SQL中仍有Noob:)将帖子标记为已回答。:)哦,是的-为什么我没有想到它。谢谢谢谢分享。这是我可以看到的,“应该”吗?ISNULL求值表达式,而不是特定对象。毫无疑问,SUM将忽略NULL。然而,SUM(ISNULL(…)比ISNULL(SUM(…)更可取,因为它不会捕获聚合null的ANSI警告。