这是我的SQL
SELECT K.SUBTOTAL_PERHOSP - G.SUBTOTAL_PERHOSP AS SOA
,K.SUBTOTAL_NOTHOSP - G.SUBTOTAL_NOTHOSP AS Disapproved
,G.SUBTOTAL_HB_TOHOSP + H.MEDICARE_INCENTIVES - H.MI_PAYABLES AS HApproved
,D.APPROVED
,k.fldvat_perhosp
,B.CONTROL_CODE
,F.OTHER_REMARKS
,(G.SUBTOTAL_HB_TOHOSP + H.MEDICARE_INCENTIVES - H.MI_PAYABLES) + (
SELECT sum(D.APPROVED)
FROM SYS_IN_PATIENT_MTBL B
LEFT JOIN SYS_IP_BILLPROC_PROFFEES_MTBL D ON B.CONTROL_CODE = D.CONTROL_CODE
WHERE B.CONTROL_CODE IN ('IP435168', 'IP435171')
) - G.MPI_PERHOSP AS NetPPD
,C.GP
,B.HOSPITAL_CODE
,B.DATE_RCVD
,B.DUE_DATE
FROM SYS_IN_PATIENT_MTBL B
INNER JOIN SYS_IP_BILLPROC_COMPUTATION_MTBL K ON B.CONTROL_CODE = K.CONTROL_CODE
INNER JOIN SYS_IP_BILLPROC_DEDUCTION_MTBL G ON B.CONTROL_CODE = G.CONTROL_CODE
LEFT JOIN SYS_IP_BILLPROC_REMARKS_MTBL H ON B.CONTROL_CODE = H.CONTROL_CODE
LEFT JOIN SYS_IP_BILLPROC_PROFFEES_MTBL D ON B.CONTROL_CODE = D.CONTROL_CODE
LEFT JOIN SYS_DOCTOR_LTBL E ON D.PF_CODE = E.DOCTOR_CODE
LEFT JOIN SYS_IP_BILLPROC_INFORMATION_MTBL F ON B.CONTROL_CODE = F.CONTROL_CODE
INNER JOIN SYS_HOSPITALS_LTBL J ON B.HOSPITAL_CODE = J.HOSPITAL_CODE
INNER JOIN SYS_HOSPITALS_LTBL C ON B.HOSPITAL_CODE = C.HOSPITAL_CODE
WHERE B.CONTROL_CODE IN ('IP435168', 'IP435171')
这是输出
注意列NetPPD
输出错误的值
肯定是
4,250.70
4,250.70
17,975.21
17,975.21
我怎么能实现这个,因为如果我使用多个ID它会添加该列中的所有内容,我不能在一个仅返回一个值的sql中使用group
它一定是
HApproved + Approved(600 + 800) = 4,250.70 (Same Control Code IP435168)
HApproved + Approved(600 + 1300) = 17,975.21 (Same Control Code IP435171)
注意:那些行不会合并,因为表格的最右侧有一个唯一的列我只是不包含在这里
答案 0 :(得分:1)
如果我理解正确,问题是您要将所有CONTROL_CODE总计相加而不是按代码拆分它们。要解决此问题,请将“已批准的总计”子查询移至FROM部分,并将其加入控制代码。不确定医院代码在这方面的作用是什么,将其排除在外并不是您的直接问题,但您可能希望将其添加到联接中。如果您共享表定义,我可以做得更好,但下面应该有所帮助:
SELECT K.SUBTOTAL_PERHOSP - G.SUBTOTAL_PERHOSP as SOA
, K.SUBTOTAL_NOTHOSP - G.SUBTOTAL_NOTHOSP as Disapproved
, G.SUBTOTAL_HB_TOHOSP + H.MEDICARE_INCENTIVES - H.MI_PAYABLES as HApproved
, D.APPROVED
, k.fldvat_perhosp
, B.CONTROL_CODE
, F.OTHER_REMARKS
,(G.SUBTOTAL_HB_TOHOSP + H.MEDICARE_INCENTIVES - H.MI_PAYABLES) + APRV.Approved - G.MPI_PERHOSP as NetPPD
, C.GP
, B.HOSPITAL_CODE
, B.DATE_RCVD
, B.DUE_DATE
from SYS_IN_PATIENT_MTBL B
INNER JOIN SYS_IP_BILLPROC_COMPUTATION_MTBL K ON B.CONTROL_CODE = K.CONTROL_CODE
INNER JOIN SYS_IP_BILLPROC_DEDUCTION_MTBL G ON B.CONTROL_CODE = G.CONTROL_CODE
LEFT JOIN SYS_IP_BILLPROC_REMARKS_MTBL H ON B.CONTROL_CODE = H.CONTROL_CODE
LEFT JOIN SYS_IP_BILLPROC_PROFFEES_MTBL D ON B.CONTROL_CODE = D.CONTROL_CODE
LEFT JOIN SYS_DOCTOR_LTBL E ON D.PF_CODE = E.DOCTOR_CODE
LEFT JOIN SYS_IP_BILLPROC_INFORMATION_MTBL F ON B.CONTROL_CODE = F.CONTROL_CODE
INNER JOIN SYS_HOSPITALS_LTBL J ON B.HOSPITAL_CODE = J.HOSPITAL_CODE
INNER JOIN SYS_HOSPITALS_LTBL C ON B.HOSPITAL_CODE = C.HOSPITAL_CODE
LEFT OUTER JOIN
(select B.CONTROL_CODE, sum(D.APPROVED) as Approved
FROM SYS_IN_PATIENT_MTBL B
LEFT JOIN SYS_IP_BILLPROC_PROFFEES_MTBL D ON B.CONTROL_CODE = D.CONTROL_CODE
WHERE B.CONTROL_CODE IN ('IP435168','IP435171')
GROUP BY B.CONTROL_CODE
) AS APRV ON APRV.CONTROL_CODE = B.CONTROL_CODE
where B.CONTROL_CODE IN ('IP435168','IP435171')