Solved

Each GROUP BY expression must contain at least one column that is not an outer reference

Posted on 2011-03-14
4
1,460 Views
Last Modified: 2012-05-11
Hello,
I need help understanding why the following code fails with an error message of
Msg 164, Level 15, State 1, Procedure spDistributionTableSupportTeamInsert, Line 6
Each GROUP BY expression must contain at least one column that is not an outer reference.
I did fix it after doing some research by removing @TASKORDER from the group by clause - so it is now working fine.  However, I'd like to learn why its inclusion in the group by clause caused the code to fail.  

CREATE PROCEDURE spDistributionTableSupportTeamInsert
@TASKORDER  nvarchar(50)
AS

INSERT INTO Distribution
(TaskOrder,Name, LName, OBS, Department, [Function], InitialPostedDate, LastPostedDate, 
 EligibilityStatus, [Description], Contribution, DistributionAmt)

SELECT   @TASKORDER  , [Name], [LName], [OBS], [Department], [Function], Min([MinOfInitialPostedDate]), 
		 Max([MaxOfLastPostedDate]), [EligibilityStatus], [Description],  [Contribution], 
		 [DistributionAmt] FROM vwRolledUpSupportTeam WHERE [TaskOrder] <> @TASKORDER		 
		 AND [Name] NOT IN (SELECT [Name] FROM vwShareAllocation WHERE [TaskOrder] = @TASKORDER)
         GROUP BY @TASKORDER, [Name], [LName], [OBS], [Department], [Function], [EligibilityStatus], 
         [Description],  [Contribution], [DistributionAmt]

Open in new window

0
Comment
Question by:chtullu135
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 35131362
Try giving your @TASKORDER field in the SELECT clause a name, and then use that name in place of @TASKORDER in the GROUP BY. My guess is that the GROUP BY does not like variables.
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 35131690
try this:

ie, no need to add parameter to group since it is fixed...
SELECT   @TASKORDER  , [Name], [LName], [OBS], [Department], [Function], Min([MinOfInitialPostedDate]), 
		 Max([MaxOfLastPostedDate]), [EligibilityStatus], [Description],  [Contribution], 
		 [DistributionAmt] FROM vwRolledUpSupportTeam WHERE [TaskOrder] <> @TASKORDER		 
		 AND [Name] NOT IN (SELECT [Name] FROM vwShareAllocation WHERE [TaskOrder] = @TASKORDER)
         GROUP BY [Name], [LName], [OBS], [Department], [Function], [EligibilityStatus], 
         [Description],  [Contribution], [DistributionAmt]

Open in new window

0
 

Author Comment

by:chtullu135
ID: 35138366
HainKurt

I did what you suggested before I posted the question and it worked.  I wanted to know the reason why it worked.  

Shaun_Kline:

I agree that the group by doesn't like variables.  I'm trying to understand why
0
 
LVL 51

Accepted Solution

by:
Huseyin KAHRAMAN earned 500 total points
ID: 35138619
You can group by only fields or expression containing some column
and there is no reason adding some fixed value to grouping, just it does not make any sense... just add that value to select part...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question