搂住你的sql不正确,你既然求field1 ,field2分组的话,这样field1,field2 必须在group by 中出现否则告诉你不正确。搂住你可以这样啊,这样是正确的sql 语句select sum(字段一)as t1,sum(字段二)as t2 into 临时表 
form 正表 where 条件一(字段二的条件)
group by 字段0。for example:select sum(field1),sum(field2) into temparory_table 
from table where field2 =condition1 group by field1

解决方案 »

  1.   

    to mixtrue 我也知道这样不符合 关系形 数据库。
    我的意思是, sum(字段一) 的时候假没有条件限制,假设全部100条。。但是sum(字段二)的时候有一些条件限制,比如只50条,,而结果是按照一个字段归类的。
      

  2.   

    把 where 条件一 贴贴看
      

  3.   

    to inprise_lyj 谢谢。我也是想非开写的但是结果集必须放在一张表里不知道您有没有好的方法,多谢指教。
      

  4.   

    To totodo(dagger in my hand)对于field2像你所说的只有使用stored procedure 来实现,因为这么复杂的逻辑不是一个sql语句所能过实现的。你可以在stored procedure 建立temporary table 来保存你field2 的数据然后来实现,你的复杂的逻辑。我想不能可能在一条sql中实现只是sum filed2的部分数据。除非你的数据有规律可以遵循。
      

  5.   

    T0: lynx111
    所以语句的意思应该是
    select sum(字段一) as t1, sum(select 字段二 where 字段3=1) as t2 into 临时表 from 正表 group by 字段0
      

  6.   

    对于结果insert to a table ,to realize it is very simple . First : you create a stored procedure, in stored procedure ,create a temporary table to hold your temporary data .Second: select the temporary data form the temporary table to insert it into your table which you want to insert.Third: to realize your business.
      

  7.   

    select sum(字段一),sum(case when 字段3=1 then 字段二 end)as t2 into 临时表 from 正表 group by 字段0
    sql server中没问题,祝你好运!
      

  8.   

    用case啊///// 啊,那效率,我可能要汇总近10万条记录呢。
      

  9.   

    lynx1111,谢谢你。。 ,能不能通过多条t-sql 语句。完成么?因为考虑到数据库的移植,存储过程也是在考虑之外的(而且这里逻辑相对不是复杂)所以我想通过 3条或者再多一点的t-sql 完成,,谢谢大家。
      

  10.   

    sql server 中测试:
    select sum(字段1)as aa,sum(case when 字段3=1 then 字段2 end)as bb from 正表 group by 字段0
    select sum(字段1)from 正表 
    select sum(字段2)from 正表 where 字段3=1 
    select (select sum(字段1)from 正表)as aa,(select sum(字段2)from 正表 where 字段3=1)as bb
    查询1:查询成本(相对于批处理):24.58%
    查询2:查询成本(相对于批处理):18.85%
    查询3:查询成本(相对于批处理):18.85%
    查询4:查询成本(相对于批处理):37.71%
    估计用多条语句完成效率会更低!