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 8406881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:14:05+00:00 2026-06-09T23:14:05+00:00

I have two questions. First: I am trying to combine two queries into one

  • 0

I have two questions.
First: I am trying to combine two queries into one to put in a stored procedure for report making. They are

Query 1

SELECT T0.Name AS Period, SUM(ISNULL(T2.LineTotal, 0)) AS CurrentDebtors, MAX(T1.DocRate) AS ExchangeRate,
SUM(CASE WHEN DATEDIFF(day, T1.DocDate, T1.DocDueDate) > 30
THEN T2.LineTotal END) AS NonCurrentDebtors 
FROM OFPR T0 LEFT OUTER JOIN OINV T1 ON T0.AbsEntry = T1.FinncPriod 
INNER JOIN INV1 T2 ON T1.DocEntry = T2.DocEntry WHERE YEAR(T1.DocDate) = @Year
GROUP BY T0.Name 
ORDER BY T0.Name 

Query 2

SELECT T0.Name AS Period, SUM(T1.DocTotal) AS TurnoverMonth
FROM dbo.OFPR T0 LEFT OUTER JOIN dbo.ORCT T1 ON T0.AbsEntry = T1.FinncPriod
WHERE YEAR(T1.DocDate) =  @Year
GROUP BY T0.Name
ORDER BY T0.Name

I have combined them into this:

SELECT T0.Name AS Period, SUM(ISNULL(T2.LineTotal, 0)) AS CurrentDebtors, MAX(T1.DocRate) AS ExchangeRate,
SUM(CASE WHEN DATEDIFF(day, T1.DocDate, T1.DocDueDate) > 30
THEN T2.LineTotal END) AS NonCurrentDebtors , SUM(ISNULL(T3.DocTotal, 0)) AS TurnoverMonth
FROM OFPR T0 LEFT OUTER JOIN OINV T1 ON T0.AbsEntry = T1.FinncPriod 
INNER JOIN INV1 T2 ON T1.DocEntry = T2.DocEntry 
JOIN ORCT T3 ON T0.AbsEntry = T3.FinncPriod
WHERE YEAR(T1.DocDate) = @Year
GROUP BY T0.Name 
ORDER BY T0.Name 

The problem is that while the results of the separate 2 queries above are correct, the combined query above returns incorrect values which are very large in amount. How do I combine the two properly?
Second: Query 1 also takes some time to execute if there is very large data, any way to improve its efficiency? Using Microsoft SQL Server 2008

  • 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-09T23:14:07+00:00Added an answer on June 9, 2026 at 11:14 pm

    Due to the nature of joins you are bound to get duplicated aggregations if you try to aggregate two or more tables simultaneously. There is probably n:m relation between T2 and T3 when connected through TO. To solve this you might use cte or derived tables – this rewrite uses derived table for T3 to retrieve one row exactly per TO.AbsEntry, thus removing duplication.

    SELECT T0.Name AS Period, 
           SUM(T2.LineTotal) AS CurrentDebtors, 
           MAX(T1.DocRate) AS ExchangeRate,
           SUM(CASE WHEN DATEDIFF(day, T1.DocDate, T1.DocDueDate) > 30
                    THEN T2.LineTotal 
               END) AS NonCurrentDebtors, 
           T3.TurnoverMonth
      FROM OFPR T0 
     INNER JOIN OINV T1
        ON T0.AbsEntry = T1.FinncPriod 
     INNER JOIN INV1 T2 
        ON T1.DocEntry = T2.DocEntry
     INNER JOIN 
     (
        SELECT ORCT.FinncPriod, 
               SUM(ORCT.DocTotal) AS TurnoverMonth
          FROM ORCT
         WHERE YEAR(ORCT.DocDate) = @Year
         GROUP BY ORCT.FinncPriod
     )  T3 
        ON T0.AbsEntry = T3.FinncPriod
     WHERE YEAR(T1.DocDate) = @Year
     GROUP BY T0.Name, T3.TurnoverMonth
     ORDER BY T0.Name
    

    Another possibility is that you need to filter ORCT table by year, which is omitted in combined query. For performance reasons you might consider expanding that filter to date-range test, to allow Sql Server to use index on DocPeriod:

    where DocDate >= convert(datetime, convert(varchar(20), @year) + '0101')
      and DocDate < dateadd (year, 1, 
                             convert(datetime, convert(varchar(20), @year) + '0101'))
    

    Note I’ve added TurnoverMonth to group by. This does not change anything because there will be exaclty one row per T3.

    I’ve also removed isnull() test from sum, because null values are removed from sum() anyway. If you want to replace final null results with zeros, envelope sum() in isnull().

    EDIT: forgot to mention that I’ve changed left join to inner join because the condition on T1 alters the meaning of the join to inner join (missing rows cannot be matched by anything except is null/is not null). If you actually need outer join, move condition to ON clause.

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

Sidebar

Related Questions

I have two questions. (First) I'm trying to make it where whenever the iPhone
I have two questions.They both are concerning a void in C++,which I am trying
I have two questions, actaully... First off, Why cant I do this: List<Object> object
Hey everyone, I want to start using Scheme and I have two questions. First,
I have two questions: 1) I am trying to fill a form's fields with
I have two questions here. How can I show the header of the pop
I have two questions. How can I get the spinner in the figure below
I have two questions. Does it make sense when I have to choice of
I have two questions. Question1: How can we add a reference to a third-party
I have two questions. I know it is possible to declare class objects in

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.