相同列中的总值和返回值

时间:2011-03-15 12:15:36

标签: sql sql-server function

在我的 SQL 中,我有一个像这样的列

SUM(ISNULL(CAST(A.ODENEN_ANAPARA AS FLOAT),0)+ISNULL(CAST(A.FAIZ AS FLOAT),0)+
      ISNULL(CAST(A.BSMV AS FLOAT),0)+ISNULL(CAST(A.GECIKME_FAIZ AS FLOAT),0)+
      ISNULL(CAST(A.GECIKME_BSMV AS FLOAT),0)) AS 'YATAN'

我有一个函数获得这样的另一个值;

USE [Example]
GO
/****** Object:  UserDefinedFunction [dbo].[fngcodeme]    Script Date: 03/15/2011 11:52:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[fngcodeme]
(
    @HESAP INT,@BAS DATE, @BIT DATE,@DOV INT
)
RETURNS FLOAT

AS
BEGIN

RETURN( 
    SELECT SUM(TUTAR)
    FROM YAZ..MARDATA.M_GHAREKET
    WHERE TEMEL_HESAP = @HESAP
    AND DOVIZ_KOD = @DOV
    AND REF_KOD = 'GC'
    AND BACAK_GRUP = 'PERT'
    AND ISL_KOD = 1
    AND ISL_TAR >= @BAS 
    AND ISL_TAR <= @BIT
)
END

我想要的是同一列上的第一个SQL和函数的总和。

(例如:“Yatan”是A,函数返回值是B,我想要一个列A + B)

Best Regards, Soner

1 个答案:

答案 0 :(得分:1)

select
   SUM(ISNULL(CAST(A.ODENEN_ANAPARA AS FLOAT),0)+ISNULL(CAST(A.FAIZ AS FLOAT),0)+
      ISNULL(CAST(A.BSMV AS FLOAT),0)+ISNULL(CAST(A.GECIKME_FAIZ AS FLOAT),0)+
      ISNULL(CAST(A.GECIKME_BSMV AS FLOAT),0)) 
   + dbo.fngcodeme(@HESAP,@BAS,@BIT,@DOV)
from ...