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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:22:36+00:00 2026-06-05T02:22:36+00:00

I am struggling with Group By and Null , as below. I have two

  • 0

I am struggling with Group By and Null, as below.

I have two tables like below

Table : 1 accountmast       
companyID   accname             category    
102         PURCHASE ACCOUNT    Purchase Account    
102         LOCAL PURCHASE      Purchase Account    
102         SALES ACCOUNT       Sales Account   

Table: 2 ledger     
companyID   name                debit   credit
102         PURCHASE ACCOUNT    4742.3  
102         LOCAL PURCHASE      51106   
102         SALES ACCOUNT               8010
102         SALES ACCOUNT               4330000
102         PURCHASE ACCOUNT    5480000 

And I have queried as below:

select  
case    
when a.catagory ='Purchase Account' then    
l.name  
end as PurchaseAccount, 

case    
when a.catagory ='Purchase Account' then    
sum(coalesce(l.debit,0))-sum(coalesce(l.credit,0) ) 
end as PurAmt,  

case    
when a.catagory = 'Sales Account' then  
l.name      
end as SalesAccount,    
case    
when a.catagory = 'Sales Account' then  
sum(coalesce(l.credit,0))-sum(coalesce(l.debit,0) ) 
end as SalesAmt 

from ledger l join accountmast a    
on l.companyID=a.companyID  
and l.name = a.accname  
where l.companyID=102   
and a.catagory IN('Purchase Account','Sales Account')   
group by l.name,a.catagory  

And the result is:

Purchase Account    PurAmt          Sales Account   SalesAmt
LOCAL PURCHASE      51106.00         NULL           NULL
PURCHASE ACCOUNT    5484742.30       NULL           NULL
NULL                NULL            SALES ACCOUNT  4338010.00

And the requirement is:

Purchase Account    PurAmt           Sales Account   SalesAmt
LOCAL PURCHASE      51106.00         SALES ACCOUNT   4338010.00
PURCHASE ACCOUNT    5484742.30      

What is the solution?.

If I use Group By then it allows Null value to associate tables’ columns.

If I use MAX, MIN then it shows the single records. What do I have to do?

If anyone has better solution please suggest.

  • 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-05T02:22:37+00:00Added an answer on June 5, 2026 at 2:22 am
    WITH    q AS
            (
            SELECT  am.category, l.name,
                    CASE am.category WHEN 'Purchase Account' THEN amt ELSE -amt END
            FROM    accountmast am
            CROSS APPLY
                    (
                    SELECT  name, SUM(COALESCE(credit, 0) - COALESCE(debit, 0)) AS amt
                    FROM    ledger l
                    WHERE   l.name = am.accname
                            AND l.companyId = am.companyId
                    GROUP BY
                            name
                    ) l
            WHERE   am.companyId = 102
            )
    SELECT  l1.name, l1.amt, l2.name, l2.amt
    FROM    (
            SELECT  *, ROW_NUMBER() OVER (ORDER BY name) AS rn
            FROM    q
            WHERE   category = 'Purchase account'
            ) l1
    FULL JOIN
            (
            SELECT  *, ROW_NUMBER() OVER (ORDER BY name) AS rn
            FROM    q
            WHERE   category = 'Sales account'
            ) l2
    ON      l2.rn = l1.rn
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I'm struggling with the following problem. I have two tables one with employee
I have a table that looks like this: Id (PK, int, not null) ReviewedBy
Struggling getting a query to work…….. I have two tables:- tbl.candidates: candidate_id agency_business_unit_id tbl.candidate_employment_tracker
been struggling with an issue now for a day or two. I have an
I have on table named players , then other tables named tries , conversions
I am really struggling with the issue below and need some help...I have found
Really struggling to work this out... I have a text file with data like
I'm really struggling with this query. I have 4 tables (http://oberto.co.nz/db-sql.png): Invoice_Payement, Invoice, Client
I have a database with 3 tables. The first table contains students with fields
I've been struggling with this all day long! In the GemFile, I have: group

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.