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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:07:42+00:00 2026-05-23T10:07:42+00:00

I have an Oracle SQL query with which I want to fectch totals of

  • 0

I have an Oracle SQL query with which I want to fectch totals of several amount columns and some descriptive columns. In short I want output like:

INDBDebnmbr INInvoicenmbr INOpenAmount
40          10            100
40          14            125
35          20            200
60          21            75

My Querystring:

SELECT TRIM(A.ACCOUNTNUMBER) AS INDBDebnmbr
     , TRIM(A.VOUCHER) AS INinvoicenmbr
     , A.DATE_ AS INinvoiceDate
     , A.DUEDATE AS INinvoiceDueDate
     , A.TXT AS INDescription
     , A.EXCHANGECODE AS INCurrencyCode
     , subq.AMOUNTMST AS INOriginalamount
     , subq.SETTLEAMOUNTMST AS INpaidAmount
     , subq.OPENAMOUNT AS INOpenAmount
FROM (
            SELECT  DEBTRANS.VOUCHER AS VOUCHER, 
                    SUM(DEBTRANS.AMOUNTMST) AS AMOUNTMST, 
                    SUM(DEBTRANS.SETTLEAMOUNTMST) AS SETTLEAMOUNTMST, 
                    SUM(DEBTRANS.AMOUNTMST - DEBTRANS.SETTLEAMOUNTMST) AS OPENAMOUNT
            FROM XAL_SUPERVISOR.DEBTRANS DEBTRANS 
            WHERE DEBTRANS.OPEN = 1 
            AND DEBTRANS.TRANSTYPE <> 9 
            AND (DEBTRANS.AMOUNTMST - DEBTRANS.SETTLEAMOUNTMST) <> 0 
            AND DEBTRANS.DATASET = 'FIK'
            GROUP BY DEBTRANS.VOUCHER
        ) subq, 
       XAL_SUPERVISOR.DEBTRANS A 
WHERE subq.VOUCHER = A.VOUCHER 
GROUP BY    A.VOUCHER, 
            A.ACCOUNTNUMBER,
            A.DATE_, 
            A.DUEDATE, 
            A.TXT, 
            A.EXCHANGECODE,
            subq.AMOUNTMST, 
            subq.SETTLEAMOUNTMST, 
            subq.OPENAMOUNT

I do however get

INDBDEBNMBR   ININVOICENMBR  ININVOICEDATE  ININVOICEDUEDATE INOPENAMOUNT
10            2022903        12-2-2011 0:00 24-2-2011 0:00   110
35            2022903        11-2-2011 0:00 23-2-2011 0:00   110
20            2022903        17-2-2011 0:00 1-3-2011 0:00    110
10            2022903        14-2-2011 0:00 26-2-2011 0:00   110
29211         2022903        14-2-2011 0:00 26-2-2011 0:00   11

How do I get the correct SUMS by DEBTRANS.VOUCHER with the descriptive columns?

Thanks in advance,

Mike

  • 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-05-23T10:07:42+00:00Added an answer on May 23, 2026 at 10:07 am

    You’re unnecessarily joining back to the table to get your additional columns. I’m guessing that the problem is that you aren’t repeating the filter conditions for the outer instance of the table.

    Does this do what you want?

            SELECT  DEBTRANS.VOUCHER AS VOUCHER, 
                    SUM(DEBTRANS.AMOUNTMST) AS AMOUNTMST, 
                    SUM(DEBTRANS.SETTLEAMOUNTMST) AS SETTLEAMOUNTMST, 
                    SUM(DEBTRANS.AMOUNTMST - DEBTRANS.SETTLEAMOUNTMST) AS OPENAMOUNT
                    TRIM(DEBTRANS.ACCOUNTNUMBER) AS INDBDebnmbr
                  , DEBTRANS.DATE_ AS INinvoiceDate
                  , DEBTRANS.DUEDATE AS INinvoiceDueDate
                  , DEBTRANS.TXT AS INDescription
                  , DEBTRANS.EXCHANGECODE AS INCurrencyCode
            FROM XAL_SUPERVISOR.DEBTRANS DEBTRANS 
            WHERE DEBTRANS.OPEN = 1 
            AND DEBTRANS.TRANSTYPE <> 9 
            AND (DEBTRANS.AMOUNTMST - DEBTRANS.SETTLEAMOUNTMST) <> 0 
            AND DEBTRANS.DATASET = 'FIK'
            GROUP BY DEBTRANS.VOUCHER
                    ,DEBTRANS.ACCOUNTNUMBER
                    ,DEBTRANS.DATE_
                    ,DEBTRANS.DUEDATE
                    ,DEBTRANS.TXT
                    ,DEBTRANS.EXCHANGECODE
    

    If not, then presumably one or more of those additional columns does not have a unique value by voucher. In this case, you need to decide how you want to select which value to display for each group. You could for example select the maximum value as shown below, but does that make sense for your businesss logic?

            SELECT  DEBTRANS.VOUCHER AS VOUCHER, 
                    SUM(DEBTRANS.AMOUNTMST) AS AMOUNTMST, 
                    SUM(DEBTRANS.SETTLEAMOUNTMST) AS SETTLEAMOUNTMST, 
                    SUM(DEBTRANS.AMOUNTMST - DEBTRANS.SETTLEAMOUNTMST) AS OPENAMOUNT
                    MAX(TRIM(DEBTRANS.ACCOUNTNUMBER)) AS INDBDebnmbr
                  , MAX(DEBTRANS.DATE_) AS INinvoiceDate
                  , MAX(DEBTRANS.DUEDATE) AS INinvoiceDueDate
                  , MAX(DEBTRANS.TXT) AS INDescription
                  , MAX(DEBTRANS.EXCHANGECODE) AS INCurrencyCode
            FROM XAL_SUPERVISOR.DEBTRANS DEBTRANS 
            WHERE DEBTRANS.OPEN = 1 
            AND DEBTRANS.TRANSTYPE <> 9 
            AND (DEBTRANS.AMOUNTMST - DEBTRANS.SETTLEAMOUNTMST) <> 0 
            AND DEBTRANS.DATASET = 'FIK'
            GROUP BY DEBTRANS.VOUCHER
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query which runs perfectly well on both Oracle and SQL
I have a following oracle sql query in which START_DATE is a number column
I have an SQL file which I want to parse and execute in oracle
As I had written in title, I have SQL query, run on Oracle DB,
I'm getting crazy with this problem: I have got some Oracle SQL-Reports to redesign
I have several Oracle queries that perform well when run through SQL*PLUS. However when
I have an oracle sql query select distinct tab1.col1, tab2.col1 from table1 tab1 join
I have the following in a SQL Server query which I have to convert
I have SQL SELECT query which returns a lot of rows, and I have
I have a managed bean which makes SQL queries to Oracle database. This is

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.