现在我有两个sql,分别如下:其中需要得到第一个sql中的件数(count(distinct code))和钱(sum(money))减去第二个后的件数和钱,怎样通过一个sql来实现呢?
sql1:
select code,
       (select date from c where c.code=a.code) date,
       money
  from a
sql2:
select code,
       (select date from d where d.code=a.code) date,
       money
  from b不要嵌套一层以上了。谢谢!

解决方案 »

  1.   


    --试一下:select code, c.date - b.date date, c.money - b.money money
      from a, b, c
     where c.code = a.code
       and b.code = a.code;
      

  2.   


    --改一下:select a.code code, c.date - b.date date, c.money - b.money money
      from a, b, c
     where c.code = a.code
       and b.code = a.code;
      

  3.   

    select a.code code, sum(c.date - b.date) date, sum(c.money - b.money) money
    from a, b, c
    where c.code = a.code and b.code = a.code
    group by a.code
      

  4.   

    楼主的sql有点问题:
      select date from c where c.code=a.code
    这句返回的date是唯一值吗??
      

  5.   

    贴错了
    应该是下面的这句sql的返回值应该不唯一  select date from d where d.code=a.code
      

  6.   

    修改一下,希望验证一下:
    select a.code code, sum(c.date - d.date) date, sum(c.money - d.money) money 
    from a,c,d
    where c.code = a.code and d.code = a.code 
    group by a.code