|
2#

楼主 |
发表于 2011-10-19 12:56:35
|
只看该作者
通过自定义函数改进:- CREATE FUNCTION dbo.f装备月份能耗数据完成(@DeviceID int, @YEAR int, @MONTH int)
- RETURNS NVARCHAR(3) AS
- BEGIN
- DECLARE @RET NVARCHAR(3)
- DECLARE @R bit
- SELECT TOP 1 @R=已完成
- FROM (
- SELECT 已完成 FROM dbo.t能耗办公楼 WHERE (装备序号=@DeviceID AND 年度 = @YEAR AND 月份=@MONTH) UNION
- SELECT 已完成 FROM dbo.t能耗囤船 WHERE (装备序号=@DeviceID AND 年度 = @YEAR AND 月份=@MONTH) UNION
- SELECT 已完成 FROM dbo.t能耗海巡艇 WHERE (装备序号=@DeviceID AND 年度 = @YEAR AND 月份=@MONTH) UNION
- SELECT 已完成 FROM dbo.t能耗车辆 WHERE (装备序号=@DeviceID AND 年度 = @YEAR AND 月份=@MONTH)
- ) AS E
-
- SET @RET= CASE WHEN @R=1 THEN N'已完成' WHEN @R=0 THEN N'未完成' ELSE N'未填报' END
- RETURN @RET
- END
- GO
- CREATE FUNCTION dbo.f装备年度各月能耗数据完成(@YEAR int, @DeptID varchar(20), @DeviceType int)
- RETURNS TABLE AS
- RETURN
- (
- SELECT D.装备序号, D.装备类型编号, P.部门, P.部门标识, L.名称, T.装备类型, @YEAR AS 年度,
- dbo.f装备月份能耗数据完成(D.装备序号, @YEAR, 1) AS [1],
- dbo.f装备月份能耗数据完成(D.装备序号, @YEAR, 2) AS [2],
- dbo.f装备月份能耗数据完成(D.装备序号, @YEAR, 3) AS [3],
- dbo.f装备月份能耗数据完成(D.装备序号, @YEAR, 4) AS [4],
- dbo.f装备月份能耗数据完成(D.装备序号, @YEAR, 5) AS [5],
- dbo.f装备月份能耗数据完成(D.装备序号, @YEAR, 6) AS [6],
- dbo.f装备月份能耗数据完成(D.装备序号, @YEAR, 7) AS [7],
- dbo.f装备月份能耗数据完成(D.装备序号, @YEAR, 8) AS [8],
- dbo.f装备月份能耗数据完成(D.装备序号, @YEAR, 9) AS [9],
- dbo.f装备月份能耗数据完成(D.装备序号, @YEAR, 10) AS [10],
- dbo.f装备月份能耗数据完成(D.装备序号, @YEAR, 11) AS [11],
- dbo.f装备月份能耗数据完成(D.装备序号, @YEAR, 12) AS [12]
- FROM dbo.t装备 AS D INNER JOIN
- dbo.v装备列表 AS L ON D.装备序号 = L.装备序号 INNER JOIN
- dbo.t部门 AS P ON L.一级部门标识 = P.部门标识 INNER JOIN
- dbo.t装备类型 AS T ON D.装备类型编号 = T.装备类型编号
- WHERE (D.装备类型编号=@DeviceType OR (@DeviceType=0 AND D.装备类型编号 IN (1, 2, 3, 4)))
- AND (一级部门标识=@DeptID OR @DeptID='' OR UPPER(@DeptID)='ALL')
- AND (D.审核 = 1) AND (D.报废 <> 1) AND (D.闲置 <> 1)
- )
- GO
复制代码 |
|