工单报表制作SQL语句,怎么查?

ak47zy 2010-10-17 08:03:50
1.现有2个表:organizationunits(公司单位表) 、 workOrder(工单表)

2.我想制作报表,要查询出来的格式如下:

一级地区 二级地区 三级地区 工单数
惠普亚洲区 惠普中国区 惠普北京区 1

惠普亚洲区 惠普中国区 惠普上海区 1

惠普亚洲区 惠普韩国区 0

微软亚洲区 微软中国区 微软北京区 1

微软亚洲区 微软中国区 微软上海区 2

微软亚洲区 微软韩国区 0


以下是代码
/****** 对象:  Table [dbo].[organizationunits]    脚本日期: 10/17/2010 19:51:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[organizationunits](
[unitCode] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[unitName] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[unitLevel] [int] NULL,
[parentCode] [varchar](6) COLLATE Chinese_PRC_CI_AS NULL,
CONSTRAINT [PK_organizationunits] PRIMARY KEY CLUSTERED
(
[unitCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'单位编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'organizationunits', @level2type=N'COLUMN',@level2name=N'unitCode'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'单位名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'organizationunits', @level2type=N'COLUMN',@level2name=N'unitName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'单位级别' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'organizationunits', @level2type=N'COLUMN',@level2name=N'unitLevel'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'父级单位编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'organizationunits', @level2type=N'COLUMN',@level2name=N'parentCode'


/****** 对象: Table [dbo].[workOrder] 脚本日期: 10/17/2010 19:52:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[workOrder](
[workCode] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[processUnit] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
CONSTRAINT [PK_workOrder] PRIMARY KEY CLUSTERED
(
[workCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'工单编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'workOrder', @level2type=N'COLUMN',@level2name=N'workCode'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'单位编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'workOrder', @level2type=N'COLUMN',@level2name=N'processUnit'


insert into organizationunits values ('010000','惠普亚洲区','1','')
insert into organizationunits values ('010100','惠普中国区','2','010000')
insert into organizationunits values ('010200','惠普韩国区','2','010000')
insert into organizationunits values ('010101','惠普北京区','3','010100')
insert into organizationunits values ('010102','惠普上海区','3','010100')

insert into organizationunits values ('020000','微软亚洲区','1','')
insert into organizationunits values ('020100','微软中国区','2','020000')
insert into organizationunits values ('020200','微软韩国区','2','020000')
insert into organizationunits values ('020101','微软北京区','3','020100')
insert into organizationunits values ('020102','微软上海区','3','020100')

insert into workOrder values ('2010101701','010101')
insert into workOrder values ('2010101702','010102')
insert into workOrder values ('2010101703','020101')
insert into workOrder values ('2010101704','020102')
insert into workOrder values ('2010101705','020102')




怎么做?
...全文
118 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ForFumm 2010-10-17
if OBJECT_ID ('[dbo].[organizationunits]','U') is not null
drop table [organizationunits]
go
CREATE TABLE [dbo].[organizationunits](
[unitCode] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[unitName] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[unitLevel] [int] NULL,
[parentCode] [varchar](6) COLLATE Chinese_PRC_CI_AS NULL,
CONSTRAINT [PK_organizationunits] PRIMARY KEY CLUSTERED
(
[unitCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
go
if OBJECT_ID ('[dbo].[workOrder]','U') is not null
drop table [workOrder]
CREATE TABLE [dbo].[workOrder](
[workCode] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[processUnit] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
CONSTRAINT [PK_workOrder] PRIMARY KEY CLUSTERED
(
[workCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

insert into organizationunits values ('010000','惠普亚洲区','1','')
insert into organizationunits values ('010100','惠普中国区','2','010000')
insert into organizationunits values ('010200','惠普韩国区','2','010000')
insert into organizationunits values ('010101','惠普北京区','3','010100')
insert into organizationunits values ('010102','惠普上海区','3','010100')

insert into organizationunits values ('020000','微软亚洲区','1','')
insert into organizationunits values ('020100','微软中国区','2','020000')
insert into organizationunits values ('020200','微软韩国区','2','020000')
insert into organizationunits values ('020101','微软北京区','3','020100')
insert into organizationunits values ('020102','微软上海区','3','020100')

insert into workOrder values ('2010101701','010101')
insert into workOrder values ('2010101702','010102')
insert into workOrder values ('2010101703','020101')
insert into workOrder values ('2010101704','020102')
insert into workOrder values ('2010101705','020102')

select 一级地区=cast(a.co1 as NCHAR(8)),二级地区=cast(a.co2 as NCHAR(8)),
三级地区=cast(isnull (a.co3,'') as NCHAR (8)) ,工单数=ISNULL(b.wcount,0) from
( select * from
(select a.unitname co1,b.unitname co2,c.unitname co3 ,c.unitcode co4 from [organizationunits] a left join [organizationunits] b on a.unitcode =b.parentcode
left join [organizationunits] c on b.unitcode=c.parentcode
) a where a.co2 is not null
) a left join
(select processUnit,COUNT(*) wcount from [workOrder] group by processUnit) b
on a.co4=b.processunit
if OBJECT_ID ('[dbo].[organizationunits]','U') is not null
drop table [organizationunits]
go
if OBJECT_ID ('[dbo].[workOrder]','U') is not null
drop table [workOrder]
一级地区 二级地区 三级地区 工单数
-------- -------- -------- -----------
惠普亚洲区 惠普中国区 惠普北京区 1
惠普亚洲区 惠普中国区 惠普上海区 1
惠普中国区 惠普北京区 0
惠普中国区 惠普上海区 0
惠普亚洲区 惠普韩国区 0
微软亚洲区 微软中国区 微软北京区 1
微软亚洲区 微软中国区 微软上海区 2
微软中国区 微软北京区 0
微软中国区 微软上海区 0
微软亚洲区 微软韩国区 0

(10 行受影响)
回复
dawugui 2010-10-17
这个比较麻烦,如果级数不定的话,参考如下的内容,自己更改:


/*
标题:SQL SERVER 2000中查询指定节点及其所有子节点的函数(表格形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/

create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go

--查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.pid = b.id and b.level = @level - 1
end
return
end
go

--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 10 行)
*/

--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
002 001 广州市
004 002 天河区

(所影响的行数为 2 行)
*/

--调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 7 行)
*/

drop table tb
drop function f_cid



@@ROWCOUNT:返回受上一语句影响的行数。
返回类型:integer。
注释:任何不返回行的语句将这一变量设置为 0 ,如 IF 语句。
示例:下面的示例执行 UPDATE 语句并用 @@ROWCOUNT 来检测是否有发生更改的行。

UPDATE authors SET au_lname = 'Jones' WHERE au_id = '999-888-7777'
IF @@ROWCOUNT = 0
print 'Warning: No rows were updated'

结果:

(所影响的行数为 0 行)
Warning: No rows were updated



/*
标题:SQL SERVER 2005中查询指定节点及其所有子节点的方法(表格形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2010-02-02
地点:新疆乌鲁木齐
*/

create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10))
insert into tb values('001' , null , N'广东省')
insert into tb values('002' , '001' , N'广州市')
insert into tb values('003' , '001' , N'深圳市')
insert into tb values('004' , '002' , N'天河区')
insert into tb values('005' , '003' , N'罗湖区')
insert into tb values('006' , '003' , N'福田区')
insert into tb values('007' , '003' , N'宝安区')
insert into tb values('008' , '007' , N'西乡镇')
insert into tb values('009' , '007' , N'龙华镇')
insert into tb values('010' , '007' , N'松岗镇')
go

DECLARE @ID VARCHAR(3)

--查询ID = '001'的所有子节点
SET @ID = '001'
;WITH T AS
(
SELECT ID , PID , NAME
FROM TB
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.PID , A.NAME
FROM TB AS A JOIN T AS B ON A.PID = B.ID
)
SELECT * FROM T ORDER BY ID
/*
ID PID NAME
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(10 行受影响)
*/

--查询ID = '002'的所有子节点
SET @ID = '002'
;WITH T AS
(
SELECT ID , PID , NAME
FROM TB
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.PID , A.NAME
FROM TB AS A JOIN T AS B ON A.PID = B.ID
)
SELECT * FROM T ORDER BY ID
/*
ID PID NAME
---- ---- ----------
002 001 广州市
004 002 天河区

(2 行受影响)
*/

--查询ID = '003'的所有子节点
SET @ID = '003'
;WITH T AS
(
SELECT ID , PID , NAME
FROM TB
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.PID , A.NAME
FROM TB AS A JOIN T AS B ON A.PID = B.ID
)
SELECT * FROM T ORDER BY ID
/*
ID PID NAME
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(7 行受影响)
*/

drop table tb

--注:除ID值不一样外,三个SQL语句是一样的。
回复
相关推荐
发帖
疑难问题
创建于2007-09-28

2.1w+

社区成员

MS-SQL Server 疑难问题
申请成为版主
帖子事件
创建了帖子
2010-10-17 08:03
社区公告
暂无公告