需要帮助将第3个表连接到Stored Proc

时间:2011-03-04 18:51:13

标签: sql join

我有一个存储过程可以很好地将两个表连接在一起。我需要从原始SP中未包含的新表中添加新字段。我要做的是为每个记录从新表中求一个字段,该记录是原始SP中父表的子记录。 我在测试查询中根据父表测试了Sum,它工作正常:

select totaldollars from TTS_EmpTime where emptimedaytotal_id='32878'

所以接下来的步骤就是整合到SP中。我这样做了,并将SP的新部分设置为粗体,以便您可以看到添加的内容。如果删除粗体部分,SP工作正常,如果不是我收到此错误:


*消息8120,级别16,状态1,过程TTS_RptTest2,第11行

列'TTS_EmpTimeDayTotal.EmployeeID'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句*中。

这是我的存储过程:

    USE [TTSTimeClock]
GO
/****** Object:  StoredProcedure [dbo].[TTS_RptTest2]    Script Date: 03/04/2011 12:29:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[TTS_RptTest2]
    @BureauID nvarchar(36),
    @CompanyID nvarchar(36),
    @DivisionID nvarchar(10) ,      
    @punchDate smalldatetime,   
    @PeriodDays integer,    
    @EmployeeID nvarchar(20) = null

As
--with DayTotals as(
    select 
        DayTotal.DivisionID,
        DayTotal.EmployeeID,
        EmpData.EmployeeFirstName AS First, 
        EmpData.EmployeeLastName AS Last,
        EmpData.employeetypeid AS EmpId,        
        DayTotal.ID as DayTotalID,  
        -- Format the Date as MM/DD DOW or 2Digit Month & 2Digit Day and the 3Char Day of the week Uppercase  
        convert(varchar(5),DayTotal.PunchDate,101) + '  ' + upper(left(datename(dw,DayTotal.Punchdate),3))as PunchDate, 
        -- Format the in and out time as non military time with AM or PM No Dates
        substring(convert(varchar(20), DayTotal.FirstDayPunch, 9), 13, 5) + ' ' + substring(convert(varchar(30), DayTotal.FirstDayPunch, 9), 25, 2)as TimeIn,
        substring(convert(varchar(20), DayTotal.LastDayPunch, 9), 13, 5) + ' ' + substring(convert(varchar(30), DayTotal.LastDayPunch, 9), 25, 2) as TimeOut,
        DayTotal.RegularHours,
        DayTotal.NonOvertimeHours,
        DayTotal.OvertimeHours,
        DayTotal.TotalDayHRS,   
        DayTotal.PeriodRegular,
        DayTotal.PeriodOtherTime,
        DayTotal.PeriodOvertime,
        DayTotal.PeriodTotal,
        **sum(cast(EmpTime.TotalDollars as float)) as TotalDayDollars** 
    from TTS_EmpTimeDayTotal as DayTotal 
         INNER JOIN TTS_PayrollEmployees AS EmpData
         ON DayTotal.EmployeeID = EmpData.EmployeeID
         **inner JOIN TTS_Emptime as EmpTime 
         ON DayTotal.id = emptime.emptimedaytotal_id**
    where
        DayTotal.BureauID = @BureauID
        AND DayTotal.CompanyID = @CompanyID
        AND (DayTotal.DivisionID = @DivisionID)
        AND daytotal.periodstart = 
                 -- Period start date
                (SELECT DISTINCT PeriodStart
                 FROM TTS_EmpTimeDayTotal 
                 WHERE(BureauID = @BureauID) AND (CompanyID = @CompanyID) AND ( (DivisionID = @DivisionID)) 
                 AND (PunchDate = @punchDate)and periodend = dateadd(d,(@PeriodDays - 1),(periodstart))) 
        AND daytotal.periodend = 
                -- Period End Date 
                (SELECT DISTINCT PeriodEnd
                 FROM TTS_EmpTimeDayTotal 
                 WHERE(BureauID = @BureauID) AND (CompanyID = @CompanyID) AND ( (DivisionID = @DivisionID)) 
                 AND (PunchDate = @punchDate)and periodend = dateadd(d,(@PeriodDays-1),(periodstart))) 
    -- Optional all employees or just one       
    AND (( @EmployeeID is Null) or (DayTotal.EmployeeID = @EmployeeID)) 
    order by Empdata.employeetypeid,DayTotal.punchdate

我根本没有分组,所以这必须是由别的东西引起的? 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

这是SQL Server吗?看起来像。您使用的是SUM聚合函数,我认为您不能使用而不使用 GROUP BY子句。你是否总是在那里有SUM,或者你是否将它与新表一起添加?

如果是后者,这可能是你的问题。

<强>更新

基于OP的评论:

  

哇,这可能是一种痛苦   像groupby field1,field2一样,   等等?就像在昏迷中划定的那样   名单。还有其他方法可以包含   这个领域会更好吗?

是的,在SQL Server中,使用聚合函数时必须明确分组。在您的情况下,一种替代方法是将分组作为子查询进行分组,并加入 ,即:

FROM TTS_EmpTimeDayTotal AS DayTotal 
INNER JOIN TTS_PayrollEmployees AS EmpData ON DayTotal.EmployeeID = EmpData.EmployeeID
INNER JOIN (SELECT EmpTimeDayTotal_id, SUM(CAST(TotalDollars AS FLOAT)) AS TotalDayDollars
            FROM TTS_Emptime
            GROUP BY EmpTimeDayTotal_id) AS EmpTime ON DayTotal.id = EmpTime.EmpTimeDayTotal_id

然后只需在SELECT列表中引用EmpTime.TotalDayDollars,而不是在那里执行SUM。