34,576
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据:[t_Meeting]
if object_id('[t_Meeting]') is not null drop table [t_Meeting]
go
create table [t_Meeting]([meeting_id] int,[subject] varchar(5),[begin_time] datetime,[end_time] datetime,[room_id] int)
insert [t_Meeting]
select 1,'会议1','2013-12-10 10:00','2013-12-10 10:00',1 union all
select 2,'会议2','2013-12-10 14:00','2013-12-10 16:00',2
--> 测试数据:[t_Room]
if object_id('[t_Room]') is not null drop table [t_Room]
go
create table [t_Room]([room_id] int,[room_name] varchar(10))
insert [t_Room]
select 1,'第一会议室' union all
select 2,'第二会议室'
--> 测试数据:[t_MeetingDep]
if object_id('[t_MeetingDep]') is not null drop table [t_MeetingDep]
go
create table [t_MeetingDep]([meeting_id] int,[dep_id] int)
insert [t_MeetingDep]
select 1,1 union all
select 1,2 union all
select 2,2 union all
select 2,4
--> 测试数据:[t_dep]
if object_id('[t_dep]') is not null drop table [t_dep]
go
create table [t_dep]([dep_id] int,[dep_name] varchar(6))
insert [t_dep]
select 1,'财务部' union all
select 2,'信息部' union all
select 3,'综合部' union all
select 4,'市场部'
--------------开始查询--------------------------
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[F_ReturnString]') and xtype in (N'FN', N'IF', N'TF'))
DROP function [dbo].[F_ReturnString]
GO
CREATE FUNCTION F_ReturnString(@id int)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @s NVARCHAR(MAX)
SET @s=''
SELECT @s=@s+','+b.dep_Name FROM t_MeetingDep a INNER JOIN t_dep b
ON a.dep_id=b.dep_id WHERE a.meeting_id=@id
RETURN RIGHT(@s,LEN(@s)-1)
END
GO
SELECT c.meeting_id AS 会议编号,c.SUBJECT AS 会议名称,a.room_name AS 会议室,b.dep_id AS 参会部门,c.begin_time AS 开始时间,c.end_time AS 结束时间 FROM t_Room a INNER JOIN
t_Meeting c ON a.room_id=c.room_id INNER JOIN
(
SELECT meeting_id,dbo.F_ReturnString(meeting_id) dep_id FROM t_MeetingDep a GROUP BY meeting_id
)b
ON c.meeting_id=b.meeting_id
/*
会议编号 会议名称 会议室 参会部门 开始时间 结束时间
----------- ----- ---------- ---------------------------- ----------------------- -----------------------
1 会议1 第一会议室 财务部,信息部 2013-12-10 10:00:00.000 2013-12-10 10:00:00.000
2 会议2 第二会议室 信息部,市场部 2013-12-10 14:00:00.000 2013-12-10 16:00:00.000
(2 行受影响)
*/
create table t_MeetingDep(meeting_id int, dep_id int, dep_name varchar(30))
insert into t_MeetingDep
select 1, 1, '财务部' union all
select 1, 2,'信息部' union all
select 2, 2,'信息部' union all
select 2, 4,'市场部'
select a.meeting_id,(select ','+b.dep_name from t_MeetingDep b
where a.meeting_id= b.meeting_id for xml path('')) from
t_MeetingDep a
group by a.meeting_id
select a.meeting_id,STUFF((select ','+b.dep_name from t_MeetingDep b
where a.meeting_id= b.meeting_id for xml path('')),1,1,'') from
t_MeetingDep a
group by a.meeting_id
create table t_Meeting(
meeting_id int,
subject varchar(20),
begin_time datetime,
end_time datetime,
room_id int
)
insert into t_Meeting
select 1 ,'会议1', '2013-12-10 10:00', '2013-12-10 10:00',1 union all
select 2 ,'会议2', '2013-12-10 14:00', '2013-12-10 16:00',2
create table t_Room(room_id int, room_name varchar(20))
insert into t_Room
select 1 , '第一会议室' union all
select 2 , '第二会议室'
create table t_MeetingDep(meeting_id int, dep_id int)
insert into t_MeetingDep
select 1, 1 union all
select 1, 2 union all
select 2, 2 union all
select 2, 4
create table 部门表(dep_id int, dep_name varchar(30))
insert into 部门表
select 1 , '财务部' union all
select 2 , '信息部' union all
select 3 , '综合部' union all
select 4 , '市场部'
go
select distinct
m.meeting_id 会议编号,
m.subject 会议名称,
r.room_name 会议室,
stuff(
(select ','+d.dep_name
from t_MeetingDep md
inner join 部门表 d
on d.dep_id = md.dep_id
where m.meeting_id = md.meeting_id
for xml path('')
),1,1,''
) as 参会部门,
m.begin_time 开始时间,
m.end_time 结束时间
from t_Meeting m
inner join t_Room r
on m.room_id = r.room_id
/*
会议编号 会议名称 会议室 参会部门 开始时间 结束时间
1 会议1 第一会议室 财务部,信息部 2013-12-10 10:00:00.000 2013-12-10 10:00:00.000
2 会议2 第二会议室 信息部,市场部 2013-12-10 14:00:00.000 2013-12-10 16:00:00.000
*/
select distinct
m.meeting_id 会议编号,
m.subject 会议名称,
r.room_name 会议室,
stuff(
(select ','+md.dep_name
from t_MeetingDep md
inner join 部门表 d
on d.dep_id = md.dep_id
where r.meeting_id = md.meeting_id
for xml path('')
),1,1,''
) as 参会部门,
m.begin_time 开始时间,
m.end_time 结束时间
from t_Meeting m
inner join t_Room r
on m.room_id = r.room_id