我有三张桌子
12-30-2016
每天有8班。每个班次都有一名医生,每班需要一些护士。护士按照医生的时间表工作。我只想计算get_shift_need_nurse(String date){}
中有多少轮班没有足够的护士。我的功能是: doctor_schedule_id| room_id | date |shift | nurse_needed|nurse_have_in_room
------------------------------------------------------------------------------
1 | 1 |12-30-2016| 1 | 3 | 2
3 | 3 |12-30-2016| 2 | 2 | 1
结果应为:
12-30-2016
因为doctor_schedule_id =4
班次1和班次2在每个房间都没有足够的护士。 nurse_schedule_id =4
不是结果,
[this, is, document, one, grumpy, two, sad, three, happy]
答案 0 :(得分:0)
COUNT(CASE WHEN nurse_needed = nurse_have_in_room then 1 ELSE NULL END) as "HasEnoughNurses"
答案 1 :(得分:0)
<强> SQL:强>
Select d.DOCTOR_SCHEDULE_ID, d.ROOM_ID, d.DT, d.SHIFT, r.ROOM_ID, r.NURSE_NEEDED, Count(*) nurse_have_in_room
From doctor_schedule d
Join room r on d.room_id = r.room_id
Join nurse_schedule n On d.doctor_schedule_id = n.doctor_schedule_id
Group by d.DOCTOR_SCHEDULE_ID, d.ROOM_ID, d.DT, d.SHIFT, r.ROOM_ID, r.NURSE_NEEDED
Having Count(*) < r.NURSE_NEEDED
测试结果:
1 3 3 12-30-2016 2 3 2 1
2 1 1 12-30-2016 1 1 3 2