Solved

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

Posted on 2011-03-14
4
1,469 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 53

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 53

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

Independent Software Vendors: 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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…

752 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