Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4034528
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:59:00+00:00 2026-05-20T11:59:00+00:00

I have a stored Procedure that works fine joining 2 tables together. I needed

  • 0

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 employees 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:59:01+00:00Added an answer on May 20, 2026 at 11:59 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a stored procedure that has to retrieve data from multiple tables something
I have a stored procedure that works fine on its own. A recent requirement
I have a line in my SQL Stored Procedure that looks like this (works
I have a stored procedure that I want to call from within another, and
I currently use SQL2008 where I have a stored procedure that fetches data from
I have a NVARCHAR(max) column in a table and a stored procedure that would
I have a stored procedure that takes a user ID and calculates their balance
I have a stored procedure that takes no parameters, and it returns two fields.
I have a stored procedure that logs some data, how can I call this
i have a stored procedure that performs a join of TableB to TableA :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.