添加2个字段,然后减去固定值

时间:2015-08-12 00:06:17

标签: sql

我想添加2个字段以获得总金额,之后我将从固定值中减去它。以下是我的代码。

CREATE VIEW [dbo].[School_Year_Amount]
AS SELECT 
ISNULL(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)), -1) AS sID,
Parent_Share_Amount.trans_name AS parent_trans_name,
OJT_Share_Amount.trans_name AS ojt_trans_name,
Parent_Share_Amount.ID_Number AS ID_Number, 
Parent_Share_Amount.adviser_info_id AS adviser_info_id, 
Parent_Share_Amount.student_fname AS student_fname, Parent_Share_Amount.student_lname AS student_lname,
Parent_Share_Amount.student_info_id AS student_info_id,
Parent_Share_Amount.amount AS parent_amount,
OJT_Share_Amount.amount AS ojt_amount,
SUM(OJT_Share_Amount.amount + Parent_Share_Amount.amount) AS total_amount,
(120000 - SUM(OJT_Share_Amount.amount + Parent_Share_Amount.amount)) AS total_balance
FROM [Parent_Share_Amount]
LEFT OUTER JOIN OJT_Share_Amount ON Parent_Share_Amount.student_info_id = OJT_Share_Amount.student_info_id;

请帮我解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:0)

CREATE VIEW [dbo].[School_Year_Amount]
AS SELECT 
ISNULL(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)), -1) AS sID,
Parent_Share_Amount.trans_name AS parent_trans_name,
OJT_Share_Amount.trans_name AS ojt_trans_name,
Parent_Share_Amount.ID_Number AS ID_Number, 
Parent_Share_Amount.adviser_info_id AS adviser_info_id, 
Parent_Share_Amount.student_fname AS student_fname,     
Parent_Share_Amount.student_lname AS student_lname,
Parent_Share_Amount.student_info_id AS student_info_id,
Parent_Share_Amount.amount AS parent_amount,
OJT_Share_Amount.amount AS ojt_amount,
isnull(OJT_Share_Amount.amount,0) + isnull(Parent_Share_Amount.amount,0) AS total_amount,
(120000 - isnull(OJT_Share_Amount.amount,0) - isnull(Parent_Share_Amount.amount,0)) AS total_balance
FROM [Parent_Share_Amount]
LEFT OUTER JOIN OJT_Share_Amount
ON Parent_Share_Amount.student_info_id = OJT_Share_Amount.student_info_id;

您不需要使用sum来添加您的值。