开发者

Need help joining 3rd table to Stored Proc

I have a stored Procedure that works fine joining 2 tables together. I needed to add a new field from a new table that was not included in the original SP. What I am trying to do is sum a field from the new table for each record that is a child record of the Parent table which is in the original SP. I tested the Sum based on th parent table in a test query and it works fine:

select totaldollars from TTS_EmpTime where emptimedaytotal_id='32878'

so then the next step would be to integrate into the SP. I did so and have set the new portions of the SP to be bold so you can see what was added. IF the bold portions are removed the SP works fine if not I get this error:


*Msg 8120, Level 16, State 1, Procedure TTS_RptTest2, Line 11

Column 'TTS_EmpTimeDayTotal.EmployeeID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause*.

Here is my Stored Proc:

    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 emplo开发者_开发技巧yees or just one       
    AND (( @EmployeeID is Null) or (DayTotal.EmployeeID = @EmployeeID)) 
    order by Empdata.employeetypeid,DayTotal.punchdate

I am not grouping at all so this must be caused by something else? Any Help will be appreciated


Is this SQL Server? Looks like it. You're using SUM, an aggregate function, which I don't believe you can use without a GROUP BY clause. Did you always have the SUM in there, or did you add it alongside the new table?

If the latter, that may well be your problem.

Update

Based on OP's comment:

Wow that could be a pain would I do somehing like groupby field1,field2, and so on? as in a coma delimited list. Is there another way to include this one field that would be better?

Yes, in SQL Server you must be explicit with groupings when using an aggregate function. One alternative in your case would be to do the grouping as a subquery, and join on that, i.e.:

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

And then simply reference EmpTime.TotalDayDollars in the SELECT list, instead of performing the SUM there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜