如何退回单排?

时间:2014-05-08 11:09:02

标签: sql-server tsql

当我检查多线功能

select * from calculation('2')

错误:

嗨谷歌之后我才知道它会返回两行(多行),但不知道如何解决这个问题可以给我任何建议

enter code here

 ALTER FUNCTION [dbo].[calculation]
(@AgentID int)
 RETURNS @CustomerPosition TABLE 
(
-- Add the column definitions for the TABLE variable here
DayAmount      Decimal   Not NULL,
MonthAmount    Decimal   NOT NULL,
YearAmount     Decimal   NOT NULL,
Position        INT      NOT NULL

)
AS
BEGIN
-- Fill the table variable with the rows for your result set
INSERT Into @CustomerPosition(DayAmount,MonthAmount,YearAmount,Position)

 Declare @DayAmount as Decimal
  set @DayAmount = (select sum(AmountRecevied) as Totoalamountperday from 
 tblcollections  
where AgentID=@AgentID and datepart(dd,Date_Time)= DATEPART(DD,GETDATE())
group by AgentID)





--SET NOCOUNT ON
 DECLARE @MonthStartDate As datetime
 DECLARE @MonthEndDate As datetime
 DECLARE @today datetime
 set @today = convert(varchar(12) , getdate() , 101) 

  SET @MonthStartDate = @today
   Declare @MonthAmount as Decimal
    set @MonthAmount = (select  sum(AmountRecevied)as Totoalamountperday from 
tblcollections  
where AgentID=@AgentID and Date_Time between   dateadd(dd,-  datepart(dd,@MonthStartDate),@MonthStartDate+1) and  dateadd(dd,-1,dateadd(mm,+1,@MonthStartDate))
           group by AgentID)



Declare @YearAmount as Decimal
    set @YearAmount=(select sum(AmountRecevied) as Totoalamountpermonth  from 
tblcollections  
where AgentID=@AgentID  and datepart(YYYY,Date_Time) =YEAR(GETDATE())
group by AgentID)

     Declare @Position as Decimal
    set @Position =  (select [RowNumber] = ROW_NUMBER() OVER(ORDER BY AgentID DESC) from 
    tblcollections  
where AgentID=@AgentID
group by AgentID
)


RETURN 
 END

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

SELECT TOP 1 ...