select sum(je)from (select top 10 je from tb_table1 where kmdm='1511')a
-
select sum(je)from (select top 10 je from tb_table2 where kmdm='1516')b--?

解决方案 »

  1.   

    如果是2005及以后的版本,试试这个:select ((select top 10 je from tb_table1 where kmdm='1511')-
    (select top 10 je from tb_table2 where kmdm='1516'))select a.je - b.je
    from 
    (
    select je,ROW_NUMBER() over(order by getdate()) rownum
    from tb_table1
    where kmdm='1511'
    )a
    inner join 
    (
    select je,ROW_NUMBER() over(order by getdate()) rownum
    from tb_table2 
    where kmdm='1516'
    )b
    on a.rownum = b.rownum
    where a.rownum = 10 and b.rownum = 10
      

  2.   

    具体意思表达清楚点。加减运算只能针对数值进行。如果你是结果集的加减  那么可以使用Union 或者union all语法。select * from t1
    union all
    select * from t2
    --会把第一个查询和第二个查询的结果集合并select * from t1
    union
    select * from t2
    --合并两个查询的结果集,并且去掉重复的数据(重复只取一行)
      

  3.   

    修改一下:select a.je - b.je
    from 
    (
    select je,ROW_NUMBER() over(order by getdate()) rownum
    from tb_table1
    where kmdm='1511'
    )a
    inner join 
    (
    select je,ROW_NUMBER() over(order by getdate()) rownum
    from tb_table2 
    where kmdm='1516'
    )b
    on a.rownum = b.rownum
    where a.rownum = 10 and b.rownum = 10
      

  4.   

    select sum(je)from (select top 10 je from tb_table1 where kmdm='1511')a -
    select sum(je)from (select top 10 je from tb_table2 where kmdm='1516')b
      

  5.   


    ROW_NUMBER()不是可识别的函数名?
      

  6.   

    表1的减去表2的你试试这个:select a.je - b.je
    from 
    (
    select je,ROW_NUMBER() over(order by getdate()) rownum
    from tb_table1
    where kmdm='1511'
    )a
    inner join 
    (
    select je,ROW_NUMBER() over(order by getdate()) rownum
    from tb_table2 
    where kmdm='1516'
    )b
    on a.rownum = b.rownum
    where a.rownum = 10 and b.rownum = 10
      

  7.   


    ROW_NUMBER()不是可识别的函数名?哦,你用的是2000对吧,我改了一下,适合2000的,你试试:
    if OBJECT_ID('tempdb..#t1') is not null
       drop table #t1
       
    if OBJECT_ID('tempdb..#t2') is not null
       drop table #t2select je,IDENTITY(int,1,1) rownum into #t1
    from tb_table1
    where kmdm='1511'select je,IDENTITY(int,1,1) rownum into #t2
    from tb_table2 
    where kmdm='1516'
    select a.je - b.je
    from #t1 a
    inner join #t2 b
    on a.rownum = b.rownum
    where a.rownum = 10 and b.rownum = 10
      

  8.   

    表1的减去表2的你试试这个:select a.je - b.je
    from 
    (
    select je,ROW_NUMBER() over(order by getdate()) rownum
    from tb_table1
    where kmdm='1511'
    )a
    inner join 
    (
    select je,ROW_NUMBER() over(order by getdate()) rownum
    from tb_table2 
    where kmdm='1516'
    )b
    on a.rownum = b.rownum
    where a.rownum = 10 and b.rownum = 10
    不对的减出来的 都是0
      

  9.   

    表1的减去表2的你试试这个:select a.je - b.je
    from 
    (
    select je,ROW_NUMBER() over(order by getdate()) rownum
    from tb_table1
    where kmdm='1511'
    )a
    inner join 
    (
    select je,ROW_NUMBER() over(order by getdate()) rownum
    from tb_table2 
    where kmdm='1516'
    )b
    on a.rownum = b.rownum
    where a.rownum = 10 and b.rownum = 10
    不对的减出来的 都是0哦 你有具体的数据不
      

  10.   


    ROW_NUMBER()不是可识别的函数名?哦,你用的是2000对吧,我改了一下,适合2000的,你试试:
    if OBJECT_ID('tempdb..#t1') is not null
       drop table #t1
       
    if OBJECT_ID('tempdb..#t2') is not null
       drop table #t2select je,IDENTITY(int,1,1) rownum into #t1
    from tb_table1
    where kmdm='1511'select je,IDENTITY(int,1,1) rownum into #t2
    from tb_table2 
    where kmdm='1516'
    select a.je - b.je
    from #t1 a
    inner join #t2 b
    on a.rownum = b.rownum
    where a.rownum = 10 and b.rownum = 10
    不对的减出来的 都是0 
      

  11.   


    ROW_NUMBER()不是可识别的函数名?哦,你用的是2000对吧,我改了一下,适合2000的,你试试:
    if OBJECT_ID('tempdb..#t1') is not null
       drop table #t1
       
    if OBJECT_ID('tempdb..#t2') is not null
       drop table #t2select je,IDENTITY(int,1,1) rownum into #t1
    from tb_table1
    where kmdm='1511'select je,IDENTITY(int,1,1) rownum into #t2
    from tb_table2 
    where kmdm='1516'
    select a.je - b.je
    from #t1 a
    inner join #t2 b
    on a.rownum = b.rownum
    where a.rownum = 10 and b.rownum = 10
    不对的减出来的 都是0 或者你有QQ吗,方便的话,帮你看看
      

  12.   

    table1表的前十条数据减去table2表的前十条数据
      

  13.   


    table1表的前十条数据减去table2表的前十条数据 比如table1表第一条数据减去table2表的第一条数据,以此类推
      

  14.   

    你这个应该是table1和table2先通过主键关联,然后看你需要哪两个字段相减就直接减就可以了