0

在此处输入图像描述 当我删除子查询时,上述查询有效。但我希望金额乘以查询中指定的表 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
4

2 回答 2

0

正如 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] 
于 2013-03-01T07:27:20.983 回答
0

同样在 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
于 2013-03-01T07:36:31.347 回答