这个要编程?

haihuan23 2010-05-09 08:54:03
有一病床Bed表:
iBed_ID sBed_ID iSection_ID iRoom_ID iBed_State iBed_type iBed_Sex Bed_UseState
92 B34 179 216 1 0 1 2
93 B36 179 216 1 0 1 2
232 B35 179 216 1 0 0 0

216 B38 179 217 1 1 2 2
94 B37 179 217 1 0 0 0
95 B39 179 217 1 0 2 2


iRoom_ID 相同表示在同一房间内.对应另一张Department 表的ID.
住在同一房间的必须是同一性别的.

iBed_Sex 表示该病床病人的性别.
Bed_useState表示该病床的使用状态.2表示有人使用,0表示没用.

请问怎么能得到空病床能入住多少男性病人,多少女性病人.
...全文
151 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
haihuan23 2010-05-09
  • 打赏
  • 举报
回复
谢谢xman_78tom(Tom)大哥.
haihuan23 2010-05-09
  • 打赏
  • 举报
回复
谢谢josy(百年树人) .你的已经很好了.

xman_78tom 2010-05-09
  • 打赏
  • 举报
回复

declare @tab table(iRoom_ID int,iBed_Sex int,Bed_UseState int);
-- 加了点数据
insert into @tab
select 216,1,2 union all select 216,1,2 union all
select 216,0,0 union all select 217,2,2 union all
select 217,0,0 union all select 217,2,2 union all
select 218,0,0 union all select 218,0,0 union all
select 218,1,2 union all select 219,2,2 union all
select 219,2,2 union all select 219,0,0;

select Sex,SUM(unused) Patient from
(select iRoom_ID,
(select top 1 iBed_Sex from @tab where iRoom_ID=t.iRoom_ID and Bed_UseState=2) sex,
(select COUNT(*) from @tab where iRoom_ID=t.iRoom_ID and Bed_UseState=0) unused
from @tab t group by iRoom_ID) t
group by sex;
/*
Sex Patient
----------- -----------
1 3
2 2
*/
百年树人 2010-05-09
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[Bed]') is not null drop table [Bed]
go
create table [Bed]([iBed_ID] int,[sBed_ID] varchar(3),[iSection_ID] int,[iRoom_ID] int,[iBed_State] int,[iBed_type] int,[iBed_Sex] int,[Bed_UseState] int)
insert [Bed]
select 92,'B34',179,216,1,0,1,2 union all
select 93,'B36',179,216,1,0,1,2 union all
select 232,'B35',179,216,1,0,0,0 union all
select 216,'B38',179,217,1,1,2,2 union all
select 94,'B37',179,217,1,0,0,0 union all
select 95,'B39',179,217,1,0,2,2

---查询---
select
sum(case when iBed_Sex=1 or iBed_Sex=0 then cnt else 0 end) as 男性可入住人数,
sum(case when iBed_Sex=2 or iBed_Sex=0 then cnt else 0 end) as 女性可入住人数
from
(
select
iRoom_ID ,max(iBed_Sex) as iBed_Sex,sum(case when Bed_UseState=0 then 1 else 0 end) cnt
from bed
group by iRoom_ID
) t

---结果---
男性可入住人数 女性可入住人数
----------- -----------
1 1

(1 行受影响)
haihuan23 2010-05-09
  • 打赏
  • 举报
回复
不是我邪恶呀. 我们医院都是老年人来做康复.而且医院病床少,医院这么安排有什么办法.
haihuan23 2010-05-09
  • 打赏
  • 举报
回复
谢谢楼上.
百年树人 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 sql_lover 的回复:]
引用 5 楼 josy 的回复:
iBed_Sex 1表示男,2表示女?

如果一间房是空的,男女都可以入住?

太邪恶了。
[/Quote]

是您太邪恶了吧
zzg_boy 2010-05-09
  • 打赏
  • 举报
回复
顶1!!!!
haihuan23 2010-05-09
  • 打赏
  • 举报
回复
是的,是楼上的意思.
0表示没有住人病床.但是房间里有人住了,只能住同一性别的.
要是整个房间没人,可以住任意性别的病人.
sql_lover 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 josy 的回复:]
iBed_Sex 1表示男,2表示女?

如果一间房是空的,男女都可以入住?
[/Quote]
太邪恶了。
sql_lover 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fredrickhu 的回复:]
iBed_Sex 怎么有0,1,2啊 难道有中性人?
[/Quote]
0表示没人吧。
sql_lover 2010-05-09
  • 打赏
  • 举报
回复
--男女都可以入住的病床
select count(1) from bed b where not exists (select 1 from bed where iRoom_ID=b.iRoom_ID where iBed_Sex=1 or iBed_Sex=2) and Bed_UseState=0
--女性可用病床
select count(1) from bed b where exists(select 1 from bed where iRoom_ID=b.iRoom_ID where ibed_sex=2) and Bed_UseState=0
--男性可用病床
select count(1) from bed b where exists(select 1 from bed where iRoom_ID=b.iRoom_ID where ibed_sex=1) and Bed_UseState=0
挨踢直男 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 josy 的回复:]

iBed_Sex 1表示男,2表示女?

如果一间房是空的,男女都可以入住?
[/Quote]
百年树人 2010-05-09
  • 打赏
  • 举报
回复
iBed_Sex 1表示男,2表示女?

如果一间房是空的,男女都可以入住?
--小F-- 2010-05-09
  • 打赏
  • 举报
回复
iBed_Sex 怎么有0,1,2啊 难道有中性人?
--小F-- 2010-05-09
  • 打赏
  • 举报
回复
select
iRoom_ID,
sum(case when Bed_UseState=0 and iBed_Sex=1 then 1 else 0 end) as '男性病人',
sum(case when Bed_UseState=0 and iBed_Sex=0 then 1 else 0 end) as '女性病人'
from
bed
group by
iRoom_ID
haihuan23 2010-05-09
  • 打赏
  • 举报
回复
简单点只要3个字段:
iRoom_ID iBed_Sex Bed_UseState
216 1 2
216 1 2
216 0 0

217 2 2
217 0 0
217 2 2

只要求有count(*) 数就可以了.
各路大大帮帮忙,都想一天了.没什么办法.
--小F-- 2010-05-09
  • 打赏
  • 举报
回复
select
Bed_UseState,iRoom_ID,
sum(case when Bed_UseState=0 and iBed_Sex=1 then 1 else 0 end) as '男性病人',
sum(case when Bed_UseState=0 and iBed_Sex=0 then 1 else 0 end) as '女性病人'
from
bed
group by
Bed_UseState,iRoom_ID

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧