表1
ID    MC    JE
1    车费   50
1    餐费   30
1    车费   20
2    车费   10
2    通讯费 100
2    车费    50
2    书报费  100
3    车费     60表2
ID   MC     JE
1    车费   70
1    加班费 100
1    车费   10
2    车费   30
2    通讯费  100
2    车费     70
3    车费     60得到结果ID     MC    JE-1   JE-2   JE差(JE-1 减 JE-2,且不等于0)
1     车费    70     80      -10
2     车费    60     100      40

解决方案 »

  1.   

    select id,mc,sum(je1) as [je1-1],sum(je2) as [je-2],sum(je1-je2) as [je-差]
    (
     select id,mc,je as je1,0 as je2 from ta where mc='车费'
     union all
     select id,mc,0 as je1,je as je2 from tb where mc='车费'
    ) t
    group by id,mc
    having sum(je1-je2)!=0
      

  2.   

    create table tb1(ID int,MC nvarchar(10),JE int)
    insert into tb1 select 1,'车费',50
    insert into tb1 select 1,'餐费',30
    insert into tb1 select 1,'车费',20
    insert into tb1 select 2,'车费',10
    insert into tb1 select 2,'通讯费',100
    insert into tb1 select 2,'车费',50
    insert into tb1 select 2,'书报费',100
    insert into tb1 select 3,'车费',60
    create table tb2(ID int,MC nvarchar(10),JE int)
    insert into tb2 select 1,'车费',70
    insert into tb2 select 1,'加班费',100
    insert into tb2 select 1,'车费',10
    insert into tb2 select 2,'车费',30
    insert into tb2 select 2,'通讯费',100
    insert into tb2 select 2,'车费',70
    insert into tb2 select 3,'车费',60
    go
    select a.id,a.mc,a.je1,b.je2,a.je1-b.je2 as je差 from(
    select id,mc,sum(je)as je1 from tb1 where mc='车费' group by id,mc
    )a inner join (
    select id,mc,sum(je)as je2 from tb2 where mc='车费' group by id,mc
    )b on a.id=b.id
    go
    drop table tb1,tb2
    /*
    id          mc         je1         je2         je差
    ----------- ---------- ----------- ----------- -----------
    1           车费         70          80          -10
    2           车费         60          100         -40
    3           车费         60          60          0(3 行受影响)*/
      

  3.   

    create table tb1(ID int,MC nvarchar(10),JE int)
    insert into tb1 select 1,'车费',50
    insert into tb1 select 1,'餐费',30
    insert into tb1 select 1,'车费',20
    insert into tb1 select 2,'车费',10
    insert into tb1 select 2,'通讯费',100
    insert into tb1 select 2,'车费',50
    insert into tb1 select 2,'书报费',100
    insert into tb1 select 3,'车费',60
    create table tb2(ID int,MC nvarchar(10),JE int)
    insert into tb2 select 1,'车费',70
    insert into tb2 select 1,'加班费',100
    insert into tb2 select 1,'车费',10
    insert into tb2 select 2,'车费',30
    insert into tb2 select 2,'通讯费',100
    insert into tb2 select 2,'车费',70
    insert into tb2 select 3,'车费',60
    go
    select a.id,a.mc,a.je1,b.je2,a.je1-b.je2 as je差 from(
    select id,mc,sum(je)as je1 from tb1 where mc='车费' group by id,mc
    )a inner join (
    select id,mc,sum(je)as je2 from tb2 where mc='车费' group by id,mc
    )b on a.id=b.id where a.je1-b.je2<>0
    go
    drop table tb1,tb2
    /*
    id          mc         je1         je2         je差
    ----------- ---------- ----------- ----------- -----------
    1           车费         70          80          -10
    2           车费         60          100         -40(2 行受影响)*/
      

  4.   


    use  City
    go
    set nocount on
    if object_id(N'tb1',N'U') is not null drop table tb1
    go
    if object_id(N'tb2',N'U') is not null drop table tb2
    gocreate table tb1(ID int,MC nvarchar(10),JE int)
    insert into tb1 select 1,'车费',50
    insert into tb1 select 1,'餐费',30
    insert into tb1 select 1,'车费',20
    insert into tb1 select 2,'车费',10
    insert into tb1 select 2,'通讯费',100
    insert into tb1 select 2,'车费',50
    insert into tb1 select 2,'书报费',100
    insert into tb1 select 3,'车费',60
    create table tb2(ID int,MC nvarchar(10),JE int)
    insert into tb2 select 1,'车费',70
    insert into tb2 select 1,'加班费',100
    insert into tb2 select 1,'车费',10
    insert into tb2 select 2,'车费',30
    insert into tb2 select 2,'通讯费',100
    insert into tb2 select 2,'车费',70
    insert into tb2 select 3,'车费',60
    goselect ID,MC,sum(je1) as [je1-1],sum(je2) as [je-2],sum(je1-je2) as [je-差] from
    (
     select ID,MC,JE as je1,0 as je2 from tb1 where mc='车费'
     union all
     select ID,MC,0 as je1,je as je2 from tb2 where mc='车费'
    ) t
    group by ID,MC
    having sum(je1-je2)!=0
    drop table tb1
    drop table tb2
    ID          MC         je1-1       je-2        je-差
    ----------- ---------- ----------- ----------- -----------
    1           车费         70          80          -10
    2           车费         60          100         -40
      

  5.   

    create table tb1(ID int,MC nvarchar(10),JE int)
    insert into tb1 select 1,'车费',50
    insert into tb1 select 1,'餐费',30
    insert into tb1 select 1,'车费',20
    insert into tb1 select 2,'车费',10
    insert into tb1 select 2,'通讯费',100
    insert into tb1 select 2,'车费',50
    insert into tb1 select 2,'书报费',100
    insert into tb1 select 3,'车费',60
    create table tb2(ID int,MC nvarchar(10),JE int)
    insert into tb2 select 1,'车费',70
    insert into tb2 select 1,'加班费',100
    insert into tb2 select 1,'车费',10
    insert into tb2 select 2,'车费',30
    insert into tb2 select 2,'通讯费',100
    insert into tb2 select 2,'车费',70
    insert into tb2 select 3,'车费',60
    goselect id , mc,
           sum(case px when 1 then je else 0 end) [JE-1],
           sum(case px when 2 then je else 0 end) [JE-2],
           sum(case px when 1 then je else -je end) [JE差]
    from
    (
    select id , mc , je , px = 1 from tb1 where mc = '车费'
    union all
    select id , mc , je , px = 2 from tb2 where mc = '车费'
    ) t
    group by id , mc
    having sum(case px when 1 then je else -je end) <> 0drop table tb1 , tb2/*
    id          mc         JE-1        JE-2        JE差         
    ----------- ---------- ----------- ----------- ----------- 
    1           车费         70          80          -10
    2           车费         60          100         -40(所影响的行数为 2 行)
    */