SELECT    
     sum([amount])
  FROM [Ex06]
where [qyid]='111049'
and 
[classcode] in
(SELECT 
 [code]
 FROM calss1
where id='c325'  or  id='c321'  
)
and [calss1] in ('1','5')根据次语句 聚合出来的数据 仅是id='c325'  的聚合 没有id='c321'  的聚合如何把两个情况全部加上 
 id='c325'  or  id='c321'  
的总聚合
???

解决方案 »

  1.   

    修正下
    SELECT    
        sum([amount]) 
      FROM [Ex06] 
    where [qyid]='111049' 
    and 
    [classcode] in 
    (SELECT 
    [code] 
    FROM calss1 
    where id='c325'  or  id='c321'  

    and [calss2] in ('1','5') 
      

  2.   

    SELECT 
    [code] 
    FROM calss1 
    where id='c325'  or  id='c321'  这样查出来包含id='c321'的数据吗?是否是其他条件限制了
      

  3.   


    SELECT   
        sum([amount])
      FROM [Ex06]
    where [qyid]='111049'
    and
    [classcode] in
    (SELECT
    [code]
    FROM calss1
    where id='c321' 
    )
    and [calss1] in ('1','5') 
    union all
    SELECT   
        sum([amount])
      FROM [Ex06]
    where [qyid]='111049'
    and
    [classcode] in
    (SELECT
    [code]
    FROM calss1
    where id='c325'
    )
    and [calss1] in ('1','5') 
      

  4.   

    表1 [calss2] in ('1','5') 和[qyid]='111049' 有id='c321'的数据?
      

  5.   

    SELECT 
    [code] 
    FROM calss1 
    where id='c325'  or  id='c321'  
    可以出来 包括 id='c325'  or  id='c321'  的相关类别
      

  6.   


    可以得到两个列国
    但要将结果sum 呢?
      

  7.   

    SELECT   
        sum([amount])
      FROM [Ex06]
    where [qyid]='111049'
    and
    [classcode] in
    (SELECT
    [code]
    FROM calss1
    where id='c321' 
    )
    and [calss1] in ('1','5') 
    union all
    SELECT   
        sum([amount])
      FROM [Ex06]
    where [qyid]='111049'
    and
    [classcode] in
    (SELECT
    [code]
    FROM calss1
    where id='c325'
    )
    and [calss1] in ('1','5')可以得到两个结果 而且准确
    但需求是是 sum 两个结果
    ??????????
      

  8.   


    select sum(a.amount) from 
    (SELECT 
        sum([amount]) as amount
      FROM [Ex06]
    where [qyid]='111049'
    and
    [classcode] in
    (SELECT
    [code]
    FROM calss1
    where id='c321'
    )
    and [calss1] in ('1','5')
    union all
    SELECT 
        sum([amount]) as amount
      FROM [Ex06]
    where [qyid]='111049'
    and
    [classcode] in
    (SELECT
    [code]
    FROM calss1
    where id='c325'
    )
    and [calss1] in ('1','5') ) a
      

  9.   

    select sum(sumamount) as summ
    from (
    SELECT  
        sum([amount]) as sumamount
      FROM [Ex06] 
    where [qyid]='111049' 
    and 
    [classcode] in 
    (SELECT 
    [code] 
    FROM calss1 
    where id='c321' 

    and [calss1] in ('1','5') 
    union all 
    SELECT  
        sum([amount]) 
      FROM [Ex06] 
    where [qyid]='111049' 
    and 
    [classcode] in 
    (SELECT 
    [code] 
    FROM calss1 
    where id='c325' 

    and [calss1] in ('1','5') 
    ) t
      

  10.   

    看来我描述还是有问题
    select sum(a.amount) from 
    (SELECT 
        sum([amount]) as amount
      FROM [Ex06]
    where [qyid]='111049'
    and
    [classcode] in
    (SELECT
    [code]
    FROM calss1
    where id='c321'
    )
    and [calss1] in ('1','5')
    union all
    SELECT 
        sum([amount]) as amount
      FROM [Ex06]
    where [qyid]='111049'
    and
    [classcode] in
    (SELECT
    [code]
    FROM calss1
    where id='c325'
    )
    and [calss1] in ('1','5') ) a是得到 某一条 即 where [qyid]='111049' 的企业汇总数据以上代码的确可以实现但 我需要的是
    按企业代码 qyid 汇总 该企业金额
      

  11.   

    Ex06qyid  amount classcode calss1该表 存在 多条企业信息
    1个企业 在该表中存在多条记录统计初 所有企业 按条件 的汇总金额按条件
    即classcode 分类编码等于 另一个表的结果集 'c321'  or 'c325' calss1 in ('1','5') 
      

  12.   

    仔细补充下Ex06  企业经营业绩表
    qyid 企业ID
     amount 金额
     classcode  区域代码
    calss1  经营方式代码-------------------
    calss1  区域代码表
    code  分类代码
    id 区域代码Ex06  企业经营业绩表
    该表 存在 多条重复企业信息 
    统计初 所有企业 按条件 的汇总金额 按条件指 
    classcode  区域代码
     =SELECT
    [code]
    FROM calss1
    where id='c321' or id='c325' 
    并且
    calss1  经营方式代码 =1 or =5
      

  13.   

    select qyid,sum(amount)
    from ex06 join calss1 on classcode=code
    where id='c321' or id='c325' and  qyid.calss1 in ('1','5')
    group by