Click here to Skip to main content
15,917,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have table as follows

CatID| Total_Amount | Amount_paying |IsCompleted
-----|--------------|---------------|------------
CA01 | 2000 | 1700 | 0

In this table I have to check if Total_Amount and amount_paying is same or not?
Where total_amount is fixed and amount_paying will get maximize after each insert statement.

So If I insert a record
CatID| Total_Amount | Amount_paying |IsCompleted
-----|-------------- |------------ |------------
CA01 | 2000 | 1700 | 0
CA01 | 2000 | 300 | 1
Here I am paying 300 , so Total_Amount = Amount_Paying and IsCompleted=1

SQL
UPDATE tbl
    		SET is_completed = CASE WHEN SUM(amount_paying) = Total_Amount THEN 1 ELSE 0 END
            WHERE tbl.branch_id =@branchid AND CatID=@catid


giving `An aggregate may not appear in the set list of an UPDATE statement.`

What I have tried:

UPDATE tbl
SET is_completed = CASE WHEN SUM(amount_paying) = Total_Amount THEN 1 ELSE 0 END
WHERE tbl.branch_id =@branchid AND CatID=@catid
Posted
Updated 25-Nov-17 4:40am

1 solution

Just one look at your table reveals some fundamental problems with your database design:
1. The `Total_Amount` field has duplicate values, e.g. 2000. If this field is dependent on CatID only, shouldn't they be in a separate table, say `tbl2`?
2. How do you know which `Is_Completed` row to insert `1` when `Total_Amount` = `Amount_Paying`? May be it should be moved to that separate table `tbl2` too.
3. The two tables `tbl` and `tbl2` can then link through CatID field.
Check this out Introduction to database design[^]
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900