总和不适合繁殖

时间:2016-04-15 15:59:20

标签: mysql

SELECT    Sum((pvc.cpt_amount) * (pvc.unit)) AS billed_amount, 
          pvc.cpt_amount, 
          pvc.unit 
FROM      payment            AS pay 
LEFT JOIN patient_insurances AS pi 
ON        pay.who_paid = pi.id 
LEFT JOIN patient_visit_cpt AS pvc 
ON        ( 
                    pay.encounter_id = pvc.id 
          AND       pay.claim_id = pvc.claim_id ) 
LEFT JOIN patient_visit AS pv 
ON        pvc.visit_id = pv.id 
WHERE 
and       pi.insurance_id = 761 
AND       pay.created_at <= '2016-04-01 23:00:00' 
AND       pay.created_at >= '2016-03-31 00:00:00' 
GROUP BY  pay.check_number, 
          pv.attending_provider_id
  

表示单位= 1且cpt_amount = 145 ,但输出结果如此   mulitplied with 4. like 1 * 145 = 580 ..请任何人给出解决方案

1 个答案:

答案 0 :(得分:0)

您的Group By可能有误。

同时在AND条款

之后删除关键字WHERE

尝试以下查询

SELECT Sum((pvc.cpt_amount) * (pvc.unit)) AS billed_amount, 
       pvc.cpt_amount,pvc.unit 
FROM payment AS pay 
LEFT JOIN patient_insurances AS pi  ON pay.who_paid = pi.id 
LEFT JOIN patient_visit_cpt AS pvc ON(pay.encounter_id = pvc.id AND pay.claim_id = pvc.claim_id) 
LEFT JOIN patient_visit AS pv ON pvc.visit_id = pv.id 
WHERE pi.insurance_id = 761 AND pay.created_at <= '2016-04-01 23:00:00' 
      AND pay.created_at >= '2016-03-31 00:00:00' 
GROUP BY  pvc.cpt_amount,pvc.unit