我有两个桌子。
我想将chart_num值用于两个表。
表Hospital_payment_data
id chart_num treatment_fees_difference treatment_fees_check_division
1 9 200000 test
2 9 100000 test
表advenced_payment
id chart_num advenced_amount
1 9 100000
我想要结果
COUNT if_treatment_fees_check_division sum_init_amount
2 200000 100000
我希望treatment_fees_check_division =“测试”计数 和 我想要treatment_fees_check_division ='test'条件总和treatment_fees_difference + advenced_amount 如何获得我想要的结果?您能提供解决方案吗?
答案 0 :(得分:1)
我认为您正在寻找以下查询-
注意:根据您的条件和数据,if_treatment_fees_check_division的总和应为300000
SELECT A.chart_num,
COUNT(*) COUN,
SUM(CASE
WHEN treatment_fees_check_division = 'test' THEN treatment_fees_difference
ELSE 0
END) if_treatment_fees_check_division,
(
SELECT SUM(advenced_amount)
FROM advenced_payment
WHERE chart_num = A.chart_num
) sum_init_amount
FROM hospital_payment_data A
GROUP BY A.chart_num