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

  • Home
  • SEARCH
  • 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 7847167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:48:28+00:00 2026-06-02T17:48:28+00:00

ALTER PROCEDURE Sp_Num_Tracking @UserID int AS BEGIN declare @lastLogonDate datetime; ALTER PROCEDURE Sp_num_Tracking @UserID

  • 0
ALTER PROCEDURE Sp_Num_Tracking 
@UserID int

AS
BEGIN
declare @lastLogonDate datetime;
ALTER PROCEDURE Sp_num_Tracking 
@UserID int

AS
BEGIN
declare @lastLogonDate datetime;
declare @CurrentLogonDate datetime;
declare @onemonthtime datetime;
declare @frmbeg int;
declare @oneweektime datetime;

select @lastLogonDate=  max( crimedate) from tblcrime where UserID=@UserID
set @CurrentLogonDate=getdate()
set @onemonthtime=dateadd(mm,-1,@CurrentLogonDate)
set @oneweektime=dateadd(dd,-7,@CurrentLogonDate)
select @frmbeg=  max(crimeID)-min(CrimeID)  from tblcrime 



SELECT count(o.crimeID)
from tblcrime o
inner join  
tblContractor ts 
on o.MainContractorID=ts.ContractorID

inner join 
tblBusiness tb 
on o.MainContractorBUID=tb.BusinessID
inner join tblservant tw on
o.servantID=tw.servantID

inner join tblUser u 
on  u.ContractorID=o.MainContractorID



where count(o.crimeID) between @lastLogonDate and @onemonthtime or count(o.crimeID) between @lastLogonDate and @oneweektime

END

But its giving an Error Like this “An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference”. which part i want to modify….Plz help

  • 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-06-02T17:48:30+00:00Added an answer on June 2, 2026 at 5:48 pm

    Any value that is determined by an aggregate on a query has to be put into HAVING as it is evaluated on the aggregated query, not on the source query. You also need to tell it what you want to group your counts by.

    Try this as an example that assumes you want to find the MAINCONTRACTORID of the person who fits your criteria:

    SELECT 
       count(o.crimeID),
       o.MainContractorID
    from tblcrime o
    inner join  
    tblContractor ts 
    on o.MainContractorID=ts.ContractorID
    
    inner join 
    tblBusiness tb 
    on o.MainContractorBUID=tb.BusinessID
    inner join tblservant tw on
    o.servantID=tw.servantID
    
    inner join tblUser u 
    on  u.ContractorID=o.MainContractorID
    
    GROUP BY 
       o.MainContractorID
    HAVING 
       count(o.crimeID) between @lastLogonDate and @onemonthtime or count(o.crimeID) between @lastLogonDate and @oneweektime
    

    NB: An aggregate is any value that is determined by a function on a set of results that will group, like COUNT, SUM, AVG..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My insert stored procedure: ALTER procedure proj_ins_all ( @proj_number INT, @usr_id INT, @download DATETIME,
I have a stored procedure as follows: ALTER PROCEDURE [dbo].[sp_web_orders_insert] ( @userId int =
Take a look at this SP. ALTER PROCEDURE [dbo].[sp_GetRecTitleVeh] AS BEGIN select a.StockNo, c.ClaimNo,
I have the following stored procedure ALTER PROCEDURE [dbo].[sp_web_orders_insert] ( @userId int = default,
ALTER PROCEDURE [dbo].[GetTimeSheetsAttendance] @WhereClause varchar AS EXEC ('Select vwEmployeeList.ID,vwEmployeeList.Code, tbGNClient.FName + '' '' +
I have sql server procedure, please see below. ALTER PROCEDURE [dbo].[uspInsertDelegate] ( @CourseID int,
I have the following SQL: ALTER PROCEDURE [dbo].[SP_Products_GetList] @CatID int, @CatName int, @SortBy varchar(50),
This is my stored procedure: ALTER PROCEDURE its.sp_WriteTransaction ( @LoginID int, @PersonID int, @BusinessID
I tried changing a default parameter value with this: ALTER PROCEDURE [dbo].[my_sp] @currentDate datetime
Your basic SP with a default parameter: ALTER PROCEDURE [usp_debug_fails] @DATA_DT_ID AS int =

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.