简体   繁体   English

在 SQL Server 2005 中添加两个具有一个匹配列名的不同表

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

My tables are like this:我的表是这样的:

#table 1 #表格1

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

#table2 #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.它给了我一些空值,但prasadhari没有出现在结果表中。

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 SQL小提琴

MS SQL Server 2008 Schema Setup : MS SQL Server 2008 架构设置

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 :查询 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看看这个工作的 SQLFiddle http://sqlfiddle.com/#!3/5bec0/7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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