Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 January SQL Puzzle: SQL Advance Query – Calculate SUM between Two Tables

SQL Puzzle: SQL Advance Query – Calculate SUM between Two Tables

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and expected output for calculating SUM between two tables in SQL Server.

Input Data for table T1:

1
2
3
4
5
6
7
Num Code
----------- ----
1 a
4 b
5 z
8 c
10 d

Input Data for table T2:

1
2
3
4
5
6
7
8
Num Code
----------- ----
44 a
16 b
10 c
26 z
30 d
20 x

Expected Output:

1
2
3
4
5
6
7
8
TotalNum Code
----------- ----
45 a
20 b
18 c
40 d
20 x
31 z

Create two sample tables:

1
2
3
4
5
6
7
CREATE TABLE T1
(Num INT, Code CHAR(1))
GO
 
CREATE TABLE T2
(Num INT, Code CHAR(1))
GO

Insert sample records:

1
2
3
4
5
6
7
8
9
INSERT INTO T1
VALUES (1,'a'),(4,'b'),(5,'z')
,(8,'c'),(10,'d')
GO
 
INSERT INTO T2
VALUES (44,'a'),(16,'b'),(10,'c')
,(26,'z'),(30,'d'),(20,'x')
GO

Solution:

1
2
3
4
5
6
7
8
9
10
11
;WITH CTE AS
(
SELECT Num, Code
FROM T1
UNION ALL
SELECT Num, Code
FROM T2
)
SELECT SUM(Num) as TotalNum, Code
FROM CTE
GROUP BY Code

Please try the different solution for this puzzle and share it via comment...

Jan 16, 2018Anvesh Patel
SQL Server 2014: Buffer Pool Extension to improve I/O throughput and Query CacheSQL Server: Update all NULL Columns with ZERO (0)
Comments: 3
  1. Kevin O
    February 7, 2018 at 5:24 pm

    SELECT
    SUM(COALESCE(t1.num,0)+COALESCE(t2.num,0)) AS total_num,
    code
    FROM t1
    FULL JOIN t2 USING (code)
    GROUP BY code

    ReplyCancel
  2. Ankush
    September 14, 2018 at 8:41 am

    SELECT COALESCE(foo.num+foo1.num, foo.num, foo1.num), foo.code
    From table_1 foo
    Left join table_2 foo1
    on foo.code = foo1.code

    ReplyCancel
  3. Gregor
    June 4, 2019 at 11:37 am

    select
    ISNULL(T1.Num,0) + ISNULL(T2.Num,0) as TotalNum,
    ISNULL(T1.Code,T2.Code) as Code
    from
    T1
    full join T2 on T1.Code = T2.Code
    order by Code

    ReplyCancel

Leave a Reply to Ankush Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

January 16, 2018 3 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....