Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9116799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:45:36+00:00 2026-06-17T04:45:36+00:00

Could someone please help me? I am trying to write a SQL query and

  • 0

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

  1. 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
  1. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:45:37+00:00Added an answer on June 17, 2026 at 4:45 am

    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.

    Select PAYMENT_SCHEDULE_MASTER_ID from (
    Select COUNT(*)  As IdCount, PAYMENT_SCHEDULE_MASTER_ID, ACTUAL , FORECAST
    from PAYMENT_SCHEDULE_DETAIL
    group by PAYMENT_SCHEDULE_MASTER_ID, ACTUAL, FORECAST) t
    where t.IdCount = 1 
    and t.ACTUAL = 0.00
    and t.FORECAST = 0.00
    

    Above was the solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Server error I'm trying to resolve. Could someone please help
Could someone please help me with loading maps using animation? I am trying to
could someone please help me out with the (simplified) code below. I'm trying to
Could someone please help me? I'm trying to create a jobobject with JOB_OBJECT_SECURITY_ONLY_TOKEN but
could someone please help me with this problem, sorry for the Language in the
Could someone please help me. I'm trying to create an xml list with all
could soemone help me with the following condition, please? I'm trying to compare $price
Could someone please help me to convert this String into a Map ... String
Could someone please help me in here, I'm just a beginner who want to
Could someone please help explain why I can't get this to work? I properly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.