--抢几分! :)--环境 create table doctor ( empid varchar(3), empname varchar(20), empsex varchar(2), empoffice varchar(10), emptitle varchar(10) )create table kw ( id int identity(1,1), office varchar(10), dutydate datetime, weekday varchar(6), morning varchar(50), noon varchar(50), night varchar(50) )insert into doctor select '101','华佗','男','内科','主任' insert into doctor select '102','扁鹊','男','外科','副主任' insert into doctor select '103','李时珍','男','神经科','医师' insert into doctor select '104','小李飞刀','男','皮肤科','医师' insert into doctor select '105','欧阳小新','男','内科','医师' insert into doctor select '106','慕容大麻花','男','内科','医师' insert into kw select '内科','2006-12-14','星期四','|101||105||106|','|105||106|','|106|' insert into kw select '外科','2006-12-14','星期四','|102|','|102|','' insert into kw select '神经科','2006-12-15','星期四','|103|','|103|','|103|' insert into kw select '皮肤科','2006-12-15','星期四','','','|104|' insert into kw select '内科','2006-12-15','星期五','|101||105|','|101||105||106|','|106|'--建立函数 create function f_add ( @office varchar(10), @dutydate datetime, @empid varchar(3) ) returns varchar(20) as begin declare @str varchar(20) set @str = '' select @str = @str + '、' + time from temptable where office = @office and dutydate = @dutydate and empid = @empid select @str = stuff(@str,1,1,'') return(@str) end--查询数据并插入临时表select office,dutydate,time ,isnull(b.empid,'') as empid into temptable from (select office,dutydate,morning,'上午' as time from kw union select office,dutydate,noon,'下午' as time from kw union select office,dutydate,night,'晚上' as time from kw ) a left join doctor b on morning like '%|'+b.empid+'|%'--查询 select a.office,a.dutydate,a.empid,b.empname,b.emptitle,dbo.f_add(a.office,a.dutydate,a.empid) as time from temptable a inner join doctor b on a.empid = b.empid group by a.office,a.dutydate,a.empid,b.empname,b.emptitle order by a.empid--结果 内科2006-12-14 00:00:00.000101华佗主任上午 内科2006-12-15 00:00:00.000101华佗主任上午、下午 外科2006-12-14 00:00:00.000102扁鹊副主任上午、下午 神经科2006-12-15 00:00:00.000103李时珍医师上午、晚上、下午 皮肤科2006-12-15 00:00:00.000104小李飞刀医师晚上 内科2006-12-14 00:00:00.000105欧阳小新医师上午、下午 内科2006-12-15 00:00:00.000105欧阳小新医师上午、下午 内科2006-12-14 00:00:00.000106慕容大麻花医师上午、下午、晚上 内科2006-12-15 00:00:00.000106慕容大麻花医师下午、晚上--删除环境 drop table doctor drop table kw drop table temptable
有个问题,在建立临时表时为什么要on morning like '%|'+b.empid+'|%' 而且为什么只on morning一个?noon和night不用吗?
你是说在这个查询中吧?--查询数据并插入临时表select office,dutydate,time ,isnull(b.empid,'') as empid into temptable from (select office,dutydate,morning,'上午' as time from kw union select office,dutydate,noon,'下午' as time from kw union select office,dutydate,night,'晚上' as time from kw ) a left join doctor b on morning like '%|'+b.empid+'|%'这个查询用的是嵌套查询 中间嵌套的查询是: select office,dutydate,morning,'上午' as time from kw union select office,dutydate,noon,'下午' as time from kw union select office,dutydate,night,'晚上' as time from kw你可以执行一下这个语句,看看结果 其实这个查询结果集有4列,office,dutydate,morning,time 也就是说,对于union连接的查询,最终结果的列名是以第一个查询为标准的 那从这个结果集中查询的话,实际上morning列就是morning,noon,night三个列的集合了 所以只写on morning like '%|'+b.empid+'|%' 就可以了,例如我可以改成这样:select office,dutydate,time ,isnull(b.empid,'') as empid into temptable from (select office,dutydate,morning as allempid,'上午' as time from kw union select office,dutydate,noon,'下午' as time from kw union select office,dutydate,night,'晚上' as time from kw ) a left join doctor b on allempid like '%|'+b.empid+'|%'
哈哈,我写出超强的查询语句,不用临时表(不用临时表是有原因的哦)select t.Office,t.DutyDate,t.EmpID,i.EmpName,i.EmpTitle, dbo.f_add(t.office,t.dutydate,t.empid) as worktime from ( select o.DutyDate,o.Office,d.EmpID,d.EmpName,d.EmpTitle,'上午' as 'worktime' from MZ_DocInfo as d inner join MZ_OnDuty as o on o.Morning like '%|'+d.EmpID+'|%' union select o.DutyDate,o.Office,d.EmpID,d.EmpName,d.EmpTitle,'下午' as 'worktime' from MZ_DocInfo as d inner join MZ_OnDuty as o on o.Noon like '%|'+d.EmpID+'|%' union select o.DutyDate,o.Office,d.EmpID,d.EmpName,d.EmpTitle,'晚上' as 'worktime' from MZ_DocInfo as d inner join MZ_OnDuty as o on o.Night like '%|'+d.EmpID+'|%' ) as t inner join MZ_DocInfo as i on t.EmpID=i.EmpID where 1=1 and t.office='内科' --and t.dutydate like '12_14_2006%' --and t.empid='101' --and i.empname='华佗' --and i.emptitle='主任' group by t.Office,t.DutyDate,t.EmpID,i.EmpName,i.EmpTitle order by t.Office,t.DutyDate,t.EmpID,i.EmpName,i.EmpTitle
要进行多条件查询,显示出医生信息和时间
create table doctor
(
empid varchar(3),
empname varchar(20),
empsex varchar(2),
empoffice varchar(10),
emptitle varchar(10)
)create table kw
(
id int identity(1,1),
office varchar(10),
dutydate datetime,
weekday varchar(6),
morning varchar(50),
noon varchar(50),
night varchar(50)
)insert into doctor select '101','华佗','男','内科','主任'
insert into doctor select '102','扁鹊','男','外科','副主任'
insert into doctor select '103','李时珍','男','神经科','医师'
insert into doctor select '104','小李飞刀','男','皮肤科','医师'
insert into doctor select '105','欧阳小新','男','内科','医师'
insert into doctor select '106','慕容大麻花','男','内科','医师'
insert into kw select '内科','2006-12-14','星期四','|101||105||106|','|105||106|','|106|'
insert into kw select '外科','2006-12-14','星期四','|102|','|102|',''
insert into kw select '神经科','2006-12-15','星期四','|103|','|103|','|103|'
insert into kw select '皮肤科','2006-12-15','星期四','','','|104|'
insert into kw select '内科','2006-12-15','星期五','|101||105|','|101||105||106|','|106|'--建立函数
create function f_add
(
@office varchar(10),
@dutydate datetime,
@empid varchar(3)
)
returns varchar(20)
as
begin
declare @str varchar(20)
set @str = ''
select @str = @str + '、' + time from temptable where office = @office and dutydate = @dutydate and empid = @empid
select @str = stuff(@str,1,1,'')
return(@str)
end--查询数据并插入临时表select office,dutydate,time ,isnull(b.empid,'') as empid
into temptable
from
(select office,dutydate,morning,'上午' as time from kw
union
select office,dutydate,noon,'下午' as time from kw
union
select office,dutydate,night,'晚上' as time from kw
) a left join doctor b
on morning like '%|'+b.empid+'|%'--查询
select a.office,a.dutydate,a.empid,b.empname,b.emptitle,dbo.f_add(a.office,a.dutydate,a.empid) as time
from temptable a inner join doctor b on a.empid = b.empid
group by a.office,a.dutydate,a.empid,b.empname,b.emptitle
order by a.empid--结果
内科2006-12-14 00:00:00.000101华佗主任上午
内科2006-12-15 00:00:00.000101华佗主任上午、下午
外科2006-12-14 00:00:00.000102扁鹊副主任上午、下午
神经科2006-12-15 00:00:00.000103李时珍医师上午、晚上、下午
皮肤科2006-12-15 00:00:00.000104小李飞刀医师晚上
内科2006-12-14 00:00:00.000105欧阳小新医师上午、下午
内科2006-12-15 00:00:00.000105欧阳小新医师上午、下午
内科2006-12-14 00:00:00.000106慕容大麻花医师上午、下午、晚上
内科2006-12-15 00:00:00.000106慕容大麻花医师下午、晚上--删除环境
drop table doctor
drop table kw
drop table temptable
就是通过那个自定义函数来聚合的!
还在研究那个函数,本人比较笨,还没懂,仔细研究ing...
而且为什么只on morning一个?noon和night不用吗?
into temptable
from
(select office,dutydate,morning,'上午' as time from kw
union
select office,dutydate,noon,'下午' as time from kw
union
select office,dutydate,night,'晚上' as time from kw
) a left join doctor b
on morning like '%|'+b.empid+'|%'这个查询用的是嵌套查询
中间嵌套的查询是:
select office,dutydate,morning,'上午' as time from kw
union
select office,dutydate,noon,'下午' as time from kw
union
select office,dutydate,night,'晚上' as time from kw你可以执行一下这个语句,看看结果
其实这个查询结果集有4列,office,dutydate,morning,time
也就是说,对于union连接的查询,最终结果的列名是以第一个查询为标准的
那从这个结果集中查询的话,实际上morning列就是morning,noon,night三个列的集合了
所以只写on morning like '%|'+b.empid+'|%'
就可以了,例如我可以改成这样:select office,dutydate,time ,isnull(b.empid,'') as empid
into temptable
from
(select office,dutydate,morning as allempid,'上午' as time from kw
union
select office,dutydate,noon,'下午' as time from kw
union
select office,dutydate,night,'晚上' as time from kw
) a left join doctor b
on allempid like '%|'+b.empid+'|%'
dbo.f_add(t.office,t.dutydate,t.empid) as worktime
from
(
select o.DutyDate,o.Office,d.EmpID,d.EmpName,d.EmpTitle,'上午' as 'worktime'
from MZ_DocInfo as d inner join MZ_OnDuty as o
on o.Morning like '%|'+d.EmpID+'|%'
union
select o.DutyDate,o.Office,d.EmpID,d.EmpName,d.EmpTitle,'下午' as 'worktime'
from MZ_DocInfo as d inner join MZ_OnDuty as o
on o.Noon like '%|'+d.EmpID+'|%'
union
select o.DutyDate,o.Office,d.EmpID,d.EmpName,d.EmpTitle,'晚上' as 'worktime'
from MZ_DocInfo as d inner join MZ_OnDuty as o
on o.Night like '%|'+d.EmpID+'|%'
) as t inner join MZ_DocInfo as i on t.EmpID=i.EmpID
where 1=1
and t.office='内科'
--and t.dutydate like '12_14_2006%'
--and t.empid='101'
--and i.empname='华佗'
--and i.emptitle='主任'
group by t.Office,t.DutyDate,t.EmpID,i.EmpName,i.EmpTitle
order by t.Office,t.DutyDate,t.EmpID,i.EmpName,i.EmpTitle