【问题标题】:SQL Query Issue Cannot perform an aggregate function on an expression containing an aggregate or a subquery99SQL 查询问题 无法对包含聚合或子查询的表达式执行聚合函数99
【发布时间】:2013-03-01 07:18:57
【问题描述】:

当我删除子查询时,上述查询有效。 但我希望金额乘以查询中指定的表 n_bank_rate 中的名义利率。 如何实现?

     select 2 as orderby, 2 as n_order2,null as exp_id ,null as company,
         'Total -' + fac.facility_type as cashflow,
         null as exp_identification_date,
         bk.bank_name as bank,null as branch,
         null as Counterparty,null as country,null as facility_type,
         bank_facility_id as bank_facility_id ,  null as curr1, 
          sum(m.amount) as curr1_amt,
         null as curr2,
         sum(m.amount * (select top 1 notional_rate from n_bank_rate where bank_id = m.bank_id order by id desc)) as curr2_amt,
         null as converstion_rate,null as due_date,null as bank_ref,null as invoice_no,null as remarks ,
         null as [term],null as [terms_type] 
        from m_forex_exposure m 
        join m_company cm on m.comp_id = cm.comp_id
        left join m_bank bk on m.bank_id = bk.id
        left join m_facility_type fac on m.bank_facility_id = fac.id 
        join n_link_exposure le on  m.exp_id = le.ref_exp_id 
        where 
        (cm.comp_main_id = 1 and cm.group_id =1 )
        and m.amount > 0
        and (m.bank_id =94) 
        and isnull(m.bank_id,0) <> 0
        and bank_facility_id in (select id as bank_facility_id from m_facility_type where facility_type  in ('EBRD','PCFC'))
        group by  bk.bank_name , bank_facility_id,fac.facility_type

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    同样在 SQLServer2005+ 中你可以使用CROSS APPLY

     select 2 as orderby, 2 as n_order2,null as exp_id ,null as company,
             'Total -' + fac.facility_type as cashflow,
             null as exp_identification_date,
             bk.bank_name as bank,null as branch,
             null as Counterparty,null as country,null as facility_type,
             bank_facility_id as bank_facility_id ,  null as curr1, 
              sum(m.amount) as curr1_amt,
             null as curr2,
             sum(m.amount * curr2_amt.notional_rate) as curr2_amt,
             null as converstion_rate,null as due_date,null as bank_ref,null as invoice_no,null as remarks ,
             null as [term],null as [terms_type] 
            from m_forex_exposure m 
            join m_company cm on m.comp_id = cm.comp_id
            left join m_bank bk on m.bank_id = bk.id
            left join m_facility_type fac on m.bank_facility_id = fac.id 
            join n_link_exposure le on  m.exp_id = le.ref_exp_id 
            CROSS APPLY (select top 1 notional_rate from n_bank_rate where bank_id = m.bank_id order by id desc) as curr2_amt (notional_rate) 
            where 
            (cm.comp_main_id = 1 and cm.group_id =1 )
            and m.amount > 0
            and (m.bank_id =94) 
            and isnull(m.bank_id,0) <> 0
            and bank_facility_id in (select id as bank_facility_id from m_facility_type where facility_type  in ('EBRD','PCFC'))
            group by  bk.bank_name , bank_facility_id,fac.facility_type
    

    【讨论】:

      【解决方案2】:

      正如 sql server 告诉你的那样......你不能运行聚合为什么有一个子查询子查询 子查询完成后,只需从整个事物中再次选择即可。

      SELECT 
      sum(curr1_amt), 
      sum(curr1_amt)*curr2_amt,
      orderby,
      n_order2,
      exp_id ,
      company,
      cashflow,
      exp_identification_date,
      bank,
      branch,
      Counterparty,
      country,
      facility_type,
      bank_facility_id ,
      curr1,
      curr2,
      converstion_rate,
      due_date,
      bank_ref,
      invoice_no,
      remarks ,
      [term],
      [terms_type] 
      FROM
      (
          SELECT
          2 as orderby,
          2 as n_order2,
          null as exp_id ,
          null as company,
          'Total -' + fac.facility_type as cashflow,
          null as exp_identification_date,
          bk.bank_name as bank,
          null as branch,
          null as Counterparty,
          null as country,
          null as facility_type,
          bank_facility_id as bank_facility_id ,
          null as curr1,
          m.amount as curr1_amt,
          null as curr2,
          curr2_amt = (select top 1 notional_rate from n_bank_rate where bank_id = m.bank_id),
          null as converstion_rate,
          null as due_date,
          null as bank_ref,
          null as invoice_no,
          null as remarks ,
          null as [term],
          null as [terms_type]           
      
          FROM 
          m_forex_exposure m   
          join m_company cm on m.comp_id = cm.comp_id          
          left join m_bank bk on m.bank_id = bk.id          
          left join m_facility_type fac on m.bank_facility_id = fac.id           
          join n_link_exposure le on  m.exp_id = le.ref_exp_id           
      
          WHERE
          (cm.comp_main_id = 1 and cm.group_id =1)          
          and m.amount > 0          
          and (m.bank_id =94)           
          and isnull(m.bank_id,0) <> 0          
          and bank_facility_id in (select id as bank_facility_id 
                           from m_facility_type 
                           where facility_type  in ('EBRD','PCFC')
                               )          
      ) AS mytable
      GROUP BY
      orderby,
      n_order2,
      exp_id ,
      company,
      cashflow,
      exp_identification_date,
      bank,
      branch,
      Counterparty,
      country,
      facility_type,
      bank_facility_id ,
      curr1,
      curr2,
      converstion_rate,
      due_date,
      bank_ref,
      invoice_no,
      remarks ,
      [term],
      [terms_type] 
      

      【讨论】:

      • 感谢您的回复。出现错误:列“mytable.curr2_amt”在选择列表中无效,因为它既不包含在聚合函数或 GROUP BY 子句中。有什么想法???
      • 抱歉忘记添加群组了 :) 查看更新后的查询 .. 注意!没有选择 * 最好具体定义您需要哪些列
      • 注意!您将必须定义自己的分组依据,因为我不知道您查询的目的,所以我对不在聚合中的其他所有内容进行分组。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      相关资源
      最近更新 更多