扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:august 来源:august.blog.51cto.com 2007年9月12日
关键字: SQL 函数 临时表 SQL Server
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GETALL]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GETALL]
GO
--根据起始时间选择设定量、实出量、地面实出量以及出现0的情况,以查看故障信息
CREATE FUNCTION GETALL(@dateStart datetime,@dateEnd datetime)
RETURNS @Result TABLE(ENo int,PlanTotal int,FactTotal int,FactOutTotal int,FactZero int,FactOutZero int)
AS
BEGIN
DECLARE @Plan TABLE(ENo int ,PlanTotal int )
DECLARE @Fact TABLE(ENo int,FactTotal int,FactOutTotal int)
DECLARE @FactZero TABLE(ENo int,FactZero int)
DECLARE @FactOutZero TABLE(ENo int,FactOutZero int)
INSERT @Plan
SELECT Electrobath_No,sum(Al_P_Plan) AS planTotal
FROM Al_Product2005 as a
WHERE (C_date between @dateStart and @dateEnd) and (Al_Status<>'0') and (Al_P_Fact<>'0')
and
(
NOT EXISTS
(
SELECT *
FROM Al_Product2005 AS b
WHERE
(a.Al_F_Count < b.Al_F_Count) AND (a.Electrobath_No = b.Electrobath_No)
AND (a.Al_P_count = b.Al_P_count) AND (a.C_Date = b.C_Date) AND (Al_Status <> 0)
)
)
GROUP BY Electrobath_No
INSERT @Fact
SELECT DISTINCT Electrobath_No,sum(Al_P_Fact) as factTotal,sum(Al_P_FactOut) as factOutTotal
FROM Al_Product2005
WHERE (C_Date between @dateStart and @dateEnd)
GROUP BY Electrobath_No
INSERT @FactZero
SELECT Electrobath_No,count(Al_P_Fact) AS FactZero
FROM Al_Product2005
WHERE (C_Date between @dateStart and @dateEnd) and (Al_P_Fact='0')
GROUP BY Electrobath_No
INSERT @FactOutZero
SELECT Electrobath_No,count(Al_P_FactOut) as FactOutZero
FROM Al_Product2005
WHERE (C_Date between @dateStart and @dateEnd) and (Al_P_FactOut='0')
GROUP BY Electrobath_No
INSERT @Result
SELECT a.ENo,PlanTotal,FactTotal,b.FactOutTotal,c.FactZero,d.FactOutZero
FROM @Plan a,@Fact b,@FactZero c,@FactOutZero d
RETURN
END
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者