SQL FUNCTIONS返回一行

时间:2016-06-29 14:25:47

标签: sql sql-server-2008

我在SQL服务器中使用Multi语句表值函数时遇到问题,我想在函数中使用下面的查询但不确定应该如何完成或者是否可以完成,任何方向都很大赞赏

  DECLARE @start_time DATETIME ='2016-6-27 07:00:00';
    DECLARE @end_time DATETIME ='2016-6-27 15:00:00';
    DECLARE @address INT = 53;
    BEGIN

        SELECT (t1-totalhopper_01)* 0.002204622621 AS HPR1,(t2-totalhopper_02 )* 0.002204622621 AS HPR2,(t3-totalhopper_03 )*0.002204622621 AS HPR3,(t4-totalhopper_04 )*0.002204622621 AS HPR4,(t5-totalhopper_05)*0.002204622621 AS HPR5,(t6-totalhopper_06 )* 0.002204622621 AS HPR6
            FROM(
                    SELECT TOP 1  ROW_NUMBER() OVER(ORDER BY totalhopper_01 ASC) AS rownuma,TotalHopper_01,TotalHopper_02,TotalHopper_03,TotalHopper_04,TotalHopper_05,TotalHopper_06 FROM mm_Cycle_Data
                     WHERE TotalHopper_01 IS NOT NULL
                       AND TotalHopper_02 IS NOT NULL  
                       AND TotalHopper_03 IS NOT NULL 
                       AND TotalHopper_05 IS NOT NULL 
                       AND TotalHopper_06 IS NOT NULL 
                       AND t_stamp=  (SELECT MIN(t_stamp)  FROM mm_Cycle_Data WHERE get_address = @address AND T_stamp BETWEEN @start_time AND @end_time)
                )a

            LEFT JOIN(

                        SELECT TOP 1  ROW_NUMBER() OVER(ORDER BY totalhopper_01 ASC) AS rownum,TotalHopper_01 AS T1,TotalHopper_02 AS t2,TotalHopper_03 AS t3 ,TotalHopper_04 AS t4,TotalHopper_05 AS t5,TotalHopper_06  AS t6 FROM mm_Cycle_Data
                         WHERE TotalHopper_01 IS NOT NULL
                           AND TotalHopper_02 IS NOT NULL  
                           AND TotalHopper_03 IS NOT NULL 
                           AND TotalHopper_05 IS NOT NULL 
                           AND TotalHopper_06 IS NOT NULL 
                           AND t_stamp=  (SELECT MAX(t_stamp)  FROM mm_Cycle_Data WHERE get_address = @address AND T_stamp BETWEEN @start_time AND @end_time)
                    )ac
            ON rownuma=rownum;

查询结果

HPR1                                    HPR2                                    HPR3                                    HPR4                                    HPR5                                    HPR6
--------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
5800.602419716689                       0.000000000000                          133.772091397038                        0.000000000000                          0.000000000000                          0.000000000000

1 个答案:

答案 0 :(得分:1)

以下是使用代码时的基本布局。请务必阅读标量和表值函数,以了解它们应该如何使用。

CREATE FUNCTION [schema].[yourFUnctionname] (@start_time datetime, @end_time datetime, @address int)    
RETURNS 
@ReturnTable table(
    column1 decimal(18,2),
    column2 varchar(16)
    --etc... do this for all return columns
)
AS
BEGIN

 --DECLARE @start_time DATETIME ='2016-6-27 07:00:00';
 --   DECLARE @end_time DATETIME ='2016-6-27 15:00:00';
 --   DECLARE @address INT = 53;
 --  BEGIN

INSERT INTO @ReturnTable (column1, column2) --make sure you use all the columns

        SELECT (t1-totalhopper_01)* 0.002204622621 AS HPR1,(t2-totalhopper_02 )* 0.002204622621 AS HPR2,(t3-totalhopper_03 )*0.002204622621 AS HPR3,(t4-totalhopper_04 )*0.002204622621 AS HPR4,(t5-totalhopper_05)*0.002204622621 AS HPR5,(t6-totalhopper_06 )* 0.002204622621 AS HPR6
            FROM(
                    SELECT TOP 1  ROW_NUMBER() OVER(ORDER BY totalhopper_01 ASC) AS rownuma,TotalHopper_01,TotalHopper_02,TotalHopper_03,TotalHopper_04,TotalHopper_05,TotalHopper_06 FROM mm_Cycle_Data
                     WHERE TotalHopper_01 IS NOT NULL
                       AND TotalHopper_02 IS NOT NULL  
                       AND TotalHopper_03 IS NOT NULL 
                       AND TotalHopper_05 IS NOT NULL 
                       AND TotalHopper_06 IS NOT NULL 
                       AND t_stamp=  (SELECT MIN(t_stamp)  FROM mm_Cycle_Data WHERE get_address = @address AND T_stamp BETWEEN @start_time AND @end_time)
                )a

            LEFT JOIN(

                        SELECT TOP 1  ROW_NUMBER() OVER(ORDER BY totalhopper_01 ASC) AS rownum,TotalHopper_01 AS T1,TotalHopper_02 AS t2,TotalHopper_03 AS t3 ,TotalHopper_04 AS t4,TotalHopper_05 AS t5,TotalHopper_06  AS t6 FROM mm_Cycle_Data
                         WHERE TotalHopper_01 IS NOT NULL
                           AND TotalHopper_02 IS NOT NULL  
                           AND TotalHopper_03 IS NOT NULL 
                           AND TotalHopper_05 IS NOT NULL 
                           AND TotalHopper_06 IS NOT NULL 
                           AND t_stamp=  (SELECT MAX(t_stamp)  FROM mm_Cycle_Data WHERE get_address = @address AND T_stamp BETWEEN @start_time AND @end_time)
                    )ac
            ON rownuma=rownum
RETURN
END
GO