我试图找到特定年份的total service costs
(想想牙医可能会做的程序)。我有一个service
表,其中包含serviceID and serviceCost
。在visit
表中,我有date, visitID, and patientID
(我想从这张表中提取日期,因为其他人没有理由从中提取日期)。另一个能够completedservice
,所以我有serviceID and visitID
(如果访问ID是2000,并且有人完成了多项服务,它将使用不同的serviceID多次列出2000次。)
我想我明白我需要做什么,但是我被抛弃了。任何帮助表示赞赏。
这是我到目前为止所做的:
Select s.serviceID, Count(s.serviceID)*s.serviceCost
From completedservice cs, service s, visit v
Where
cs.serviceID = s.serviceID
AND cs.visitID = v.visitID
AND visitDate BETWEEN '2007-01-01' AND '2007-12-31'
Group By s.serviceID;
completedService表:
completedServiceID, serviceID, visitID
1 1 1
2 2 1
服务表:
serviceID, serviceCost
1 20
2 20
访问表格
visitID visitDate
1 2007-08-10
2 2007-08-12
感谢您帮助我将所有内容拼凑在一起的指导。
答案 0 :(得分:0)
您需要将访问次数乘以每项服务的服务费用:
SELECT
SUM(tbl.total_cost) AS total
FROM
(
SELECT
s.serviceID,
COUNT(s.serviceID)*s.serviceCost AS total_cost
FROM
completedservice cs, service s, visit v
WHERE
cs.serviceID = s.serviceID
AND
cs.visitID = v.visitID
AND
visitDate BETWEEN '2007-01-01' AND '2007-12-31'
GROUP BY
s.serviceID
) AS tbl;
这假设服务成本在期间不会改变。如果更改,则需要为每个时段执行此过程。
答案 1 :(得分:0)
Select SUM(s.serviceCost)
From visit v
Join completedservice cs on cs.visitID = v.visitID
Join service s on s.serviceID = cs.serviceID
Where v.visitDate BETWEEN '2007-01-01' AND '2007-12-31'
此查询逐行执行:
Calculate total sum spent
during all visits
by finding all completed services on each visit
and finding details of each service (that was completed)
where visit date is in 2007