这两个SQL选择独立工作,但我需要帮助将它们组合成一个select语句,以生成一个包含两个查询中所有字段的单行。
选择#1:
select
'F' row_type,
sum(movement.override_pay_amt_n + tmp.b) as total_amount
from
movement,
(select coalesce(sum(driver_extra_pay.amount_n),0.0) b
from driver_extra_pay,deduct_code,movement
where driver_extra_pay.company_id = 'tms'
and driver_extra_pay.movement_id = 1
and driver_extra_pay.movement_id = movement.id
and driver_extra_pay.payee_id = movement.override_payee_id
and movement.company_id = 'tms'
and code_type = 'E'
and deduct_code.id = driver_extra_pay.deduct_code_id
and deduct_code.company_id = 'tms') tmp
where
movement.id = 1
and movement.company_id = 'tms'
选择#2:
select
'BBS' row_type,
movement.override_pay_amt,
override_pay_amt as total_amount,
movement.override_pay_amt_n,
movement.override_pay_amt_d,
movement.override_pay_amt_c,
movement.override_pay_amt_r
from
movement
where
movement.id = 1
and movement.company_id = 'tms' ;
我的组合选择:
select
'BBSALL' row_type,
movement.override_pay_amt,
override_pay_amt as total_amount,
movement.override_pay_amt_n,
movement.override_pay_amt_d,
movement.override_pay_amt_c,
movement.override_pay_amt_r,
sum(movement.override_pay_amt_n + tmp.b) as total_amount
from
movement,
(select coalesce(sum(driver_extra_pay.amount_n),0.0) b
from driver_extra_pay,deduct_code,movement
where driver_extra_pay.company_id = 'tms'
and driver_extra_pay.movement_id = 1
and driver_extra_pay.movement_id = movement.id
and driver_extra_pay.payee_id = movement.override_payee_id
and movement.id = 1
and movement.company_id = 'tms'
and code_type = 'E'
and deduct_code.id = driver_extra_pay.deduct_code_id
and deduct_code.company_id = 'tms') tmp
但我得到了这个错误......
Msg 8120,Level 16,State 1,Line 48
列' movement.override_pay_amt'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
答案 0 :(得分:0)
只需添加分组,假设您希望汇总结果。
SELECT 'BBSALL' row_type
, movement.override_pay_amt
, override_pay_amt AS total_amount
, movement.override_pay_amt_n
, movement.override_pay_amt_d
, movement.override_pay_amt_c
, movement.override_pay_amt_r
, SUM(movement.override_pay_amt_n + tmp.b) AS total_amount
FROM movement
, (SELECT COALESCE(SUM(driver_extra_pay.amount_n), 0.0) b
FROM driver_extra_pay
, deduct_code
, movement
WHERE driver_extra_pay.company_id = 'tms'
AND driver_extra_pay.movement_id = 1
AND driver_extra_pay.movement_id = movement.id
AND driver_extra_pay.payee_id = movement.override_payee_id
AND movement.id = 1
AND movement.company_id = 'tms'
AND code_type = 'E'
AND deduct_code.id = driver_extra_pay.deduct_code_id
AND deduct_code.company_id = 'tms'
) tmp
GROUP BY row_type
, movement.override_pay_amt
, override_pay_amt
, movement.override_pay_amt_n
, movement.override_pay_amt_d
, movement.override_pay_amt_c
, movement.override_pay_amt_r