Hi,Here is my table structure,;with Users as (select 1 as UserId, 2 as PendingAmount,10 as AvailableAmount union allselect 2 as UserId, 4 as PendingAmount,12 as AvailableAmount union allselect 3 as UserId, 3 as PendingAmount,8 as AvailableAmount union allselect 4 as UserId, 26 as PendingAmount,30 as AvailableAmount union allselect 5 as UserId, 24 as PendingAmount,20 as AvailableAmount union allselect 6 as UserId, 70 as PendingAmount,100 as AvailableAmount )select * from users;;with users_transaction as ( select 1 as UserId, 3 as refund, 0 as status union allselect 1 as UserId, 2 as refund, 0 as status union allselect 2 as UserId, 5 as refund, 0 as status union allselect 2 as UserId, 10 as refund, 0 as status union allselect 3 as UserId, 13 as refund, 0 as status union allselect 1 as UserId, 13 as refund, 1 )select * from users_transaction
I need to sum the refund amount from table "users_transaction" group by UserId where status = 0 and add the amount into the "Users" table column "PendingAmount " based on the userId.Once updated the PendingAmount then have to make the corresponding status as 1 on the users_transaction table.Is is possible to do without looping the users_transaction table? idf yes Please give me some sample queryExpected Output : ;with Users as (select 1 as UserId, 7 as PendingAmount,10 as AvailableAmount union allselect 2 as UserId, 19 as PendingAmount,12 as AvailableAmount union allselect 3 as UserId, 16 as PendingAmount,8 as AvailableAmount union allselect 4 as UserId, 26 as PendingAmount,30 as AvailableAmount union allselect 5 as UserId, 24 as PendingAmount,20 as AvailableAmount union allselect 6 as UserId, 70 as PendingAmount,100 as AvailableAmount )select * from users;;with users_transaction as ( select 1 as UserId, 3 as refund, 1 as status union allselect 1 as UserId, 2 as refund,1 as status union allselect 2 as UserId, 5 as refund, 1 as status union allselect 2 as UserId, 10 as refund, 1 as status union allselect 3 as UserId, 13 as refund, 1 as status union allselect 1 as UserId, 13 as refund, 1 )select * from users_transaction