Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find UserName in Different Table Using UserId

I have two tables. One is the userInfo table another is the income expenses table, This table has multiple columns. I want to summation these columns using GROUP BY and try to show TotalIncome, Totalexpenses and userName that it.

Here is the query:

    select Pa.LoginID, Sum(CB.AFDC+CB.ChildSupport+CB.FoodStamps+CB.OtherIncome+CB.WagesSalary+CB.VeteransBenefit+CB.SocialSecurity+CB.Retirement+CB.GeneraAssistance) as TotalIncome ,
            
    Sum(CB.WaterSewer+CB.Transportation+CB.HouseRent+CB.FoodCost+CB.Electricity+CB.DoctorVisit+CB.ChildCare+CB.CarPayment+CB.OtherExpense) as TotalExpences
    
     from Client_BurnOuts CB
            
        Join PatientPortalLogins Pa on CB.PatientApplicationId = Pa.PatientApplicationId 
            group by Pa.PatientApplicationId

I can get only the total summation different used:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    select PatientApplicationId, Sum(AFDC+ChildSupport+FoodStamps+OtherIncome+WagesSalary+VeteransBenefit+SocialSecurity+Retirement+GeneraAssistance) as TotalIncome ,
    Sum(WaterSewer+Transportation+HouseRent+FoodCost+Electricity+DoctorVisit+ChildCare+CarPayment+OtherExpense) as TotalExpences
    from Client_BurnOuts
    group by PatientApplicationId

>Solution :

Try using a subquery:

select Pa.LoginID, TotalIncome , TotalExpences
from
    (select PatientApplicationId, 
        Sum(AFDC+ChildSupport+FoodStamps+OtherIncome+WagesSalary+VeteransBenefit+SocialSecurity+Retirement+GeneraAssistance) as TotalIncome ,
        Sum(WaterSewer+Transportation+HouseRent+FoodCost+Electricity+DoctorVisit+ChildCare+CarPayment+OtherExpense) as TotalExpences
    from Client_BurnOuts
    group by PatientApplicationId) AS cb
Join PatientPortalLogins Pa on CB.PatientApplicationId = Pa.PatientApplicationId
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading