2016-04-15 86 views
1
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,但輸出自帶像 用4.像1 * 145 = 580 ..請任一項得到溶液薩姆爲乘法

mulitplied
+0

顯示我們的DB模式,樣本數據和願望輸出。 \t請閱讀[**如何提問**](http://stackoverflow.com/help/how-to-ask) \t \t這裏是[** START **]( http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/)瞭解如何提高您的問題質量並獲得更好的答案。 –

+0

聽起來像你的「GROUP BY」不正確或不夠精確。您是否嘗試過不使用「SUM」運行查詢? – Technoh

+0

我的猜測是你的一個連接是與一對多相關的表。例如,如果每個'pay'有四個'pi',那麼你將總結四份'pvc' – Uueerdo

回答

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 
+0

'SELECT Sum((pvc.cpt_amount)*(pvc.unit))'和'GROUP BY pvc.cpt_amount,pvc.unit'沒有意義。 –

+0

我並不想爲cpt.amount和unit獲得分組。但一些循環或realtion可能是錯誤的 – vasanth252

+0

@PaulSpiegel是'SELECT總和((pvc.cpt_amount)*(pvc.unit))和GROUP BY pvc.cpt_amount,pvc.unit'不會造成sence,而是'SELECT pvc.cpt_amount ,pvc.unit和GROUP BY pvc.cpt_amount,pvc.unit'將使得搜索結果 –