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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:50:52+00:00 2026-05-28T05:50:52+00:00

I have two tables: t1 ( date d, name c(20), debit n(7), credit n(7)

  • 0

I have two tables:

  • t1 (date d, name c(20), debit n(7), credit n(7))
  • t2 (date d, name c(20), type c(25), obal n(7))

Here’s the sample data.

T1:

DATE        NAME        DEBIT   CREDIT
01.01.12    dad                 5000
01.01.12    mum                 6000
05.01.12    school      1000    
01.02.12    dad                 5000
01.02.12    mum                 6000
10.02.12    tuition     300 
10.02.12    snacks      100 
01.03.12    dad                 5000
01.03.12    mum                 6000
01.03.12    books       500 
02.03.12    rice        500 
02.03.12    vegetables  900 
03.03.12    snacks      100 
01.04.12    dad                 5000
01.04.12    mum                 6000
01.04.12    meat        200 
21.04.12    guest       800 

T2:

DATE        NAME        TYPE        OBAL
01.01.12    dr fee      medical     8000
01.01.12    medicine    medical     -10000
01.01.12    dad         income      400000
01.01.12    mum         income      450000
05.01.12    school      education   0
10.02.12    tuition     education   0
10.02.12    snacks      misc        0
01.03.12    books       education   0
02.03.12    rice        food        0
02.03.12    vegetables  food        0
01.04.12    meat        food        0
21.04.12    guest       misc        0

Sum of (obal + credit – debit) for each name grouped by type (t2) is expected. Query used and output obtained is:

SELECT 
    t2.type, obal + SUM(NVL(credit - debit, 0)) as bal 
FROM t2 
LEFT JOIN t1 ON t2.name = t1.name 
GROUP BY 
    t2.type, t2.obal

Results:

TYPE        BAL
education   -1800
food        -1600
income      420000
income      474000
medical     -10000
medical     8000
misc        -1000

My question is: why should the type income and medical comes twice in the query output in spite of GROUP BY clause being used? I’ve tried adding DISTINCT clause before the column t2.type and that too produces the same output! One thing I could observe is that the two records of type medical in table t2 has no records in t1 matching the same name and two records of type income has a non-zero obal in t2. Please help me to overcome this problem.

  • 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-28T05:50:53+00:00Added an answer on May 28, 2026 at 5:50 am

    The problem is that you’re not grouping just by type, but also by obal.

    Try:

    select type,sum(bal) as bal
    from(
      SELECT t2.type, obal + SUM(NVL(credit - debit, 0)) as bal FROM t2 LEFT JOIN t1 ;
      ON t2.name = t1.name GROUP BY t2.type, t2.obal
    )a
    group by type;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two tables: object id name object_event id object_id type date I
I have got two tables: Event +----------+---------+--------------+ | event_id | name | date |
I have two tables: Client(id,name,...) Purchase(id,item,date,client_id,...) They have their respective Model, with their validations.
I have a two tables MovieStar ( name: char(15), address:varchar(40), gender:char(1), birthdate: date, rating:float
I have two tables Say Table-A and Table-B Table-A id | Article Name |date
I have two tables: Teams id name Games id date idlocal idvisitor idlocal and
I have two tables: STUDENT GRADES ---------- ---------- id id name person_id address date
I have two tables, first table structure (data) : -id -name -title -mail -source_id
I have tables like these two test_table date student test 2012-05-31 Alice Math 2012-05-31
i have two tables Order(id, date, note) and Delivery(Id, Note, Date) I want to

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.