简体   繁体   中英

Add two different tables with one matching column name in SQL server 2005

My tables are like this:

#table 1

+---------------------------------------------+
| name     Tpens Tpencils Tbooks              |
+---------------------------------------------+
| suresh      1       2        5              |
| ramesh      3       5        7              |
| ravi        2       7        9              |
+---------------------------------------------+

#table2

+----------------------------------------+
| name    dpens  dpencils  dbooks        |
+----------------------------------------+
| ramesh    4        5        9          |
| prasad    5        6        7          |
| hari      7        8        9          |
+----------------------------------------+

I want results like

+------------------------------------------------------------------------------------------+
| name     Tpens  Tpencils Tbooks dpens dpencils dbooks                                    |
+------------------------------------------------------------------------------------------+
| suresh   1        2        5     0        0       0    no null values                    |
| ramesh   3        5        7     4        5       9    Ramesh has values in both tables  |
| ravi     2        7        9     0        0       0                                      |
| prasad   0        0        0     5        6       7                                      |
| hari     0        0        0     7        8       9                                      |
+------------------------------------------------------------------------------------------+

I wrote this query:

select table1.name,
        table1.tpens, 
        table1.tpencils ........etc......
        table2.dbooks 
from table1 
full outer join table2 on table1.name = table2.name

It gave me some null values but prasad and hari do not appear in the result table.

Something like this should do it.

select t1.tpens
    , t1.tpencils
    , t1.tbooks
    , isnull(t2.dpens, 0) as dpens
    , isnull(t2.dpencils, 0) as dpencils
    , isnull(t2.dbooks, 0) as dbooks
from #table1 t1
left join #table2 t2 on t1.name = t2.name

This should achieve what you are after:

SQL Fiddle

MS SQL Server 2008 Schema Setup :

CREATE TABLE one(
  name NVARCHAR(40) PRIMARY KEY,
  Tpens INT
 )
CREATE TABLE two(
  name NVARCHAR(40) PRIMARY KEY,
  Dpens INT
 )
 INSERT INTO one VALUES('bob', 5),('dave', 6), ('ravi',9)
 INSERT INTO two VALUES('bill', 4),('dave', 3), ('ravi',2)

Query 1 :

SELECT
   CASE WHEN one.name IS NULL THEN two.name ELSE one.name END
  ,ISNULL(one.Tpens,0)
  ,ISNULL(two.Dpens,0)
FROM one 
FULL OUTER JOIN two on one.name = two.name

Results :

|      |   |   |
|------|---|---|
| bill | 0 | 4 |
|  bob | 5 | 0 |
| dave | 6 | 3 |
| ravi | 9 | 2 |

This should solve your problem:

select 
    isnull(t1.name,t2.name) as name,
    isnull(t1.Tpens, 0) as Tpens,
    isnull(t1.Tpencils, 0) as Tpencils,
    isnull(t1.Tbooks, 0) as Tbooks,
    isnull(t2.Dpens, 0) as Dpens,
    isnull(t2.Dpencils, 0) as Dpencils,
    isnull(t2.Dbooks, 0) as Dbooks
from table1 as t1
full outer join table2 as t2 
    on t1.name = t2.name

Take a look at this working SQLFiddle http://sqlfiddle.com/#!3/5bec0/7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM