[英]A few SQL queries I can't figure out how to write
I need to create a few queries for a database with the following schema: 我需要使用以下架构为数据库创建一些查询:
Patient(**pid**,pname,address,phone)
Ward(**wid**, wname) // wid=Ward id,
Bed(**wid,bid**) // bid=Bed id
Appointment(**apid**,date,result,pid,cid) // cid=Consultant id, pid=Patient id
Consultant(**cid**,cname,clinId,phone) // clinId=Clinic ID
Allocation(**apid**,pid,wid,bid,date,ex_leave,act_leave) //ex=expected, act=actual
The queries are: 查询是:
I tried to create the first one using views like this: 我试图使用如下视图创建第一个:
create view hospital.occupied_beds as
select A.wid,count(*) as o_beds
from hospital.allocation A,hospital.bed B
where A.wid=B.wid and A.bid=B.bid and A.act_leave is null
group by A.wid;
create view hospital.all_beds as
select C.wid,count(*) as all_beds
from hospital.bed C
group by C.wid;
select distinct A.wid,all_beds-o_beds as uo_beds
from hospital.occupied_beds A, hospital.all_beds B
but this way it doesn't return wards in which all the beds are unoccupied. 但是这样一来,它就不会返回所有床都没有被占用的病房。
Please help me :) 请帮我 :)
It seems a bit poorly normalized, in terms that Allocation can specify both ward ID and bed ID. 在分配可以同时指定病房ID和病床ID的方面,这似乎标准化程度较差。 Is bed ID nullable, eg the patient hasn't been allocated a bed yet?
病床ID是否可以为空,例如,尚未为患者分配病床?
In any case, I think you need outer joins. 无论如何,我认为您需要外部联接。 I don't have a copy of MySQL available right now, but I believe you can do this:
我现在没有MySQL的副本,但我相信您可以这样做:
create view hospital.unoccupied_beds as
select B.wid,count(*) as o_beds
from hospital.allocation A right join hospital.bed B on A.wid=B.wid and A.bid=B.bid and A.act_leave is null
where A.apid is null
group by B.wid;
maybe something like this: you'll just need to calculate a litte bit: 也许是这样的:您只需要计算一点点:
Select Sub1.v1, Sub1.c1 as all, Sub2.c2 as free FROM
(SELECT wid.bed as v1,count(bid.bed) as c1 FROM bed, ward WHERE wid.ward = wid.bed
GROUP BY wid.bed) Sub1
LEFT JOIN
(SELECT wid.bed as v2,count(bid.bed) as c2 FROM bed, ward, allocation WHERE
wid.ward = wid.bed AND wid.bed = wid.allocation
AND bid.bed = bid.allocation AND act_leave.allocation IS NULL GROUP BY wid.bed) Sub2
ON Sub1.v1 = Sub2.v2
Here are three possible solutions for your questions. 这是您的问题的三种可能的解决方案。 Keep in mind that I was not going for efficiency.
请记住,我并不是要追求效率。 There are probably ways to optimize these queries a bit.
可能有一些方法可以优化这些查询。 I just wanted to give you ideas and get you going in the right direction.
我只是想给您一些想法,让您朝正确的方向前进。
For Unoccipied beds per ward: 每个病房的无病床:
select w.wname, bc.total - IFNULL(ob.occupied,0) as unoccupied
from Ward w,
(select wid, count(bid) as total from Bed group by wid) bc
left join (select wid, count(wid) as occupied from Allocation where act_leave is null group by wid) ob
on bc.wid = ob.wid
where w.wid = bc.wid
For wards with allocations for every day in March 2013 对于2013年3月每天分配的病房
select w.wid, w.wname, count(distinct(a.date)) as acount
from Ward w, Allocation a
where a.date >= '2013-03-01'
and a.date <= '2013-03-31'
and w.wid = a.wid
group by w.wid
having acount = 31
The list of consultants with most ortho appointments in descending order (most allocations on top) 拥有最多正交任命的顾问列表(按降序排列)(最多分配在最前面)
select c.cid, c.cname, count(a.apid) as apptcount
from Consultant c, Appointment p, Allocation a, Ward w
where c.cid = p.cid
and p.apid = a.apid
and a.wid = w.wid
and w.wname = 'Orthopedic'
group by c.cid
order by apptcount desc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.