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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:57:21+00:00 2026-05-16T17:57:21+00:00

Does anyone know how can I add an integer from another table to the

  • 0

Does anyone know how can I add an integer from another table to the current selected table in SQL Server?

For example:
I have 2 tables with the following information in each table

  1. tableA:

    id    username   point   status   country
    1     alvin      1       1        U.S.A
    2     alvin      1       1        U.S.A
    3     amy        1       0        Australia
    
  2. tableB:

    id    username    point
    1     amy         1
    2     alvin       1
    3     ken         1
    

How can I sum up the total points in tableA with also add in the sum points from tableB?
I tried the following code, but seem is not working and error display:

SELECT username, (COUNT(distinct a.point) + (SELECT SUM(a.point)
    FROM tableB b WHERE b.username = a.username) AS 'Points', status, country
FROM tableA
GROUP BY aco.username

And the output I expected will be:

username Points status country
alvin    3      1      U.S.A
amy      2      0      Australia
  • 1 1 Answer
  • 1 View
  • 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-16T17:57:22+00:00Added an answer on May 16, 2026 at 5:57 pm
    WITH Results(username,point)
    AS
    (
    SELECT username, point FROM TableA
    UNION ALL
    SELECT username, point FROM TableB
    )
    
    SELECT username, sum(point) AS Points FROM Results GROUP BY username
    GO
    

    EDIT

    The question has changed, so now the solution should look like this

    WITH Results(username,point,status, country)
    AS
    (
    SELECT username, point, status, country FROM TableA
    UNION ALL
    SELECT username, point, null, null FROM TableB
    )
    
    SELECT username, sum(point) AS Points, max(status), max(country) FROM Results GROUP BY username
    GO
    

    What is WITH ?
    What is UNION ?

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

Sidebar

Related Questions

Does anyone know how to add a description to a SQL Server column by
Does anyone know how I can add a context menu item that would compress
does anyone know how to add minutes to currentTimeMillis. i am passing an integer
I'm doing my first steps in MacRuby. Does anyone know how I can add
Does anyone know of a way in which I can add an extra database
does anyone know if this can be done? I can find Windows Server 2003
Does anyone know a tool that can add predefined text such as the following
Does anyone know how I can add a progress bar to a listview cell
Does anyone know how I can add a logo in the form of a
Does anyone know I can make min-height work with the latest browsers? I am

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.