Could someone please help me? I am trying to write a SQL query and having trouble with it for 2-3 days. let me define the problem first.
I have 2 tables
Payment_Schedule_Master
[PAYMENT_SCHEDULE_MASTER_ID] [int] NOT NULL, Primary key
[FPI_ID] [varchar](9) NOT NULL,
[DELETE_FLAG] [char](1) NOT NULL,
[CREATED_BY] [varchar](30) NOT NULL,
[CREATED_DATE] [datetime] NOT NULL,
[MODIFY_BY] [varchar](30) NOT NULL,
[MODIFY_DATE] [datetime] NOT NULL
Payment_Schedule_Detail
[PAYMENT_SCHEDULE_DETAIL_ID] [int] IDENTITY(1,1) NOT NULL, Primary key
[PAYMENT_SCHEDULE_MASTER_ID] [int] NOT NULL, Foreign key to master table
[PAY_YEAR] [int] NOT NULL,
[PAY_MONTH] [int] NOT NULL,
[ACTUAL] [money] NULL,
[FORECAST] [money] NULL,
[DELETE_FLAG] [char](1) NOT NULL,
[CREATED_BY] [varchar](30) NOT NULL,
[CREATED_DATE] [datetime] NOT NULL,
[MODIFY_BY] [varchar](30) NOT NULL,
[MODIFY_DATE] [datetime] NOT NULL
There is a one-to-many relationship between the two: Master has one entry and detail has many. Payment_Schedule_Detail has an id, foreign key, actual, forecast and many column. Actual and forecast will have numerical values in it.
Problem:
I want to get those Payment_Schedule_Master rows which have Actual and ForeCast equal to 0.
My Query:
I tried this query
Select
t.PAYMENT_SCHEDULE_MASTER_ID, psm.FPI_ID,
t.ActualSum, t.ForecastSum
from
(Select
SUM(Actual) As ActualSum,
SUM (forecast) AS ForecastSum,
PAYMENT_SCHEDULE_MASTER_ID
from
[dbo].[PAYMENT_SCHEDULE_DETAIL]
group by
PAYMENT_SCHEDULE_MASTER_ID) t
Inner Join
dbo.PAYMENT_SCHEDULE_MASTER psm on psm.PAYMENT_SCHEDULE_MASTER_ID = t.PAYMENT_SCHEDULE_MASTER_ID
where
t.ActualSum = t.ForecastSum
and t.ActualSum = 0
The problem with this query is that if Actual has 200 in Jan and -200 in Dec it will pick that title as well because SUM (Actual) will be 0 which is wrong.
I am not sure how to modify the query that it should only get those titles which has actual 0 and forecast 0.
Testing:
and also if anyone will let me know how to test the method?
Update: tried this query but it is taking 8 seconds.
Select
t.PAYMENT_SCHEDULE_MASTER_ID, psm.FPI_ID,
t.ActualSum, t.ForecastSum, psd.ACTUAL, psd.FORECAST
from
(Select
SUM(Actual) As ActualSum, SUM (forecast) AS ForecastSum,
PAYMENT_SCHEDULE_MASTER_ID
from
[dbo].[PAYMENT_SCHEDULE_DETAIL]
group by
PAYMENT_SCHEDULE_MASTER_ID) t
Inner Join
dbo.PAYMENT_SCHEDULE_MASTER psm on psm.PAYMENT_SCHEDULE_MASTER_ID = t.PAYMENT_SCHEDULE_MASTER_ID
Inner Join
[dbo].[PAYMENT_SCHEDULE_DETAIL] psd on psm.PAYMENT_SCHEDULE_MASTER_ID = psd.PAYMENT_SCHEDULE_MASTER_ID
where
t.ActualSum = t.ForecastSum
and t.ActualSum = 0
and psd.ACTUAL = 0
order by
psm.FPI_ID
Data And Output:
psm_id Actual ForeCast [other columns]
900 10000.00 0.00
900 -10000.00 0.00
900 0.00 0.00
912 0.00 0.00
912 0.00 0.00
912 0.00 0.00
psm_id = Payment_Schedule_Master_ID
Payment_Schedule_Master_Id 900 Sum of actual will be 0, It should not appear on the result. but 912 will appear in the result because all records were 0. I hope this helps.
thank you for the answers but it was not working at all.
In my database there was one special case which I didnt see. It was that if both actual and forecast is 0 for a specific Payment_Schedule_Id it will have only one row.
Above was the solution.