Hi,This is my first post in this forum. Greetings to all.First of all, this is a query that will create table and feed some data.quote:
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Temp_Payment_Holder]') AND type in (N'U'))DROP TABLE [dbo].[Temp_Payment_Holder]Create Table Temp_Payment_Holder( Payment_ID INT, Required_Payment MONEY, Cust_ID INT, Available_Amount MONEY)INSERT INTO Temp_Payment_Holder(Payment_ID, Required_Payment, Cust_ID, Available_Amount)SELECT 4, 10000, 2, 6400UNIONSELECT 4, 10000, 50, 3500UNIONSELECT 4, 10000, 79, 25000UNIONSELECT 4, 10000, 80, 10000UNIONSELECT 5, 35000, 76, 1800UNIONSELECT 5, 35000, 78, 5000UNIONSELECT 5, 35000, 80, 10000UNIONSELECT 6, 19000, 2, 6400UNIONSELECT 6, 19000, 79, 25000--IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Temp_Payment_Holder]') AND type in (N'U'))--DROP TABLE [dbo].[Temp_Payment_Holder]
Table StructurePayment_ID Required_Payment Cust_ID Available_Amount4 10000 2 64004 10000 50 35004 10000 79 250004 10000 80 100005 35000 76 18005 35000 78 50005 35000 80 100006 19000 2 64006 19000 79 25000 Payment_ID:FK of Payment_Schedule table which stores information of payments on specific dates.Required_Payment:Derived from Payment_Schedule table, payment amount of the current schedule. This is the amount that should be paid by customers.Cust_ID:Derived from Customers table.Available_Amount:Derived from a function, the total available balance of a customer.ScenarioA query is needed to generate a result like shown in the bottom in such a way that ...- The sum of Payment(of the result) of each Cust_ID should not exceed Available_Amount of Cust_ID
- If the total sum of Available_Amount is less than Required_Payment, it should return a null value, or no row at all.
Anticipated ResultPayment_ID Cust_ID Payment4 2 64004 50 35004 79 1005 NULL NULL6 79 19000 |