Solved

What does this error message mean?

Posted on 2010-11-30
7
274 Views
Last Modified: 2012-05-10
Getting an outer reference error on a group by expression
Error.doc
0
Comment
Question by:SPLady
  • 4
  • 3
7 Comments
 
LVL 22

Expert Comment

by:8080_Diver
ID: 34239921
Please also attach the SQL statement.  (The SQL statement is truncated in the error message text. ;-)
0
 
LVL 1

Author Comment

by:SPLady
ID: 34240032
thank you @ 8080 Diver the code is attached
SELECT DISTINCT 

                      (DATEDIFF(DD, @start, @end) + 1) - (DATEDIFF(DD, DATEADD(DD, 6 - (DATEPART(DW, @start) + @@DATEFIRST - 2) % 7, @start), DATEADD(DD, 

                      - ((DATEPART(DW, @end) + @@DATEFIRST - 2) % 7), @end)) - 1) / 7 * 2 - CASE (DATEPART(DW, @start) + @@DATEFIRST - 2) 

                      % 7 + 1 WHEN 6 THEN 2 WHEN 7 THEN 1 ELSE 2 END - CASE (DATEPART(DW, @end) + @@DATEFIRST - 2) 

                      % 7 + 1 WHEN 6 THEN 1 WHEN 7 THEN 2 ELSE 0 END AS WorkingDays, vReportData.salesOrderLine, AVG(PlanJoin.[Plan]) AS Expr1, 

                      vReportData.EnergyOrg, vReportData.transyear, vReportData.subProduct, PlanJoin.recType, PlanJoin.Month, PlanJoin.Year, 

                      vReportData.lineValue AS Expr2, vReportData.transDate

FROM         vReportData INNER JOIN

                      PlanJoin ON vReportData.recType = PlanJoin.recType AND vReportData.EnergyOrg = PlanJoin.EnergyOrg AND 

                      vReportData.transyear = PlanJoin.Year AND MONTH(vReportData.transDate) = PlanJoin.Month

GROUP BY (DATEDIFF(DD, @start, @end) + 1) - (DATEDIFF(DD, DATEADD(DD, 6 - (DATEPART(DW, @start) + @@DATEFIRST - 2) % 7, @start), DATEADD(DD, 

                      - ((DATEPART(DW, @end) + @@DATEFIRST - 2) % 7), @end)) - 1) / 7 * 2 - CASE (DATEPART(DW, @start) + @@DATEFIRST - 2) 

                      % 7 + 1 WHEN 6 THEN 2 WHEN 7 THEN 1 ELSE 2 END - CASE (DATEPART(DW, @end) + @@DATEFIRST - 2) 

                      % 7 + 1 WHEN 6 THEN 1 WHEN 7 THEN 2 ELSE 0 END, vReportData.salesOrderLine, vReportData.EnergyOrg, vReportData.transyear, 

                      vReportData.subProduct, PlanJoin.recType, PlanJoin.Month, PlanJoin.Year, vReportData.lineValue, vReportData.transDate

HAVING      (vReportData.transDate > CONVERT(DATETIME, '2010-11-15 00:00:00', 102))

Open in new window

0
 
LVL 22

Accepted Solution

by:
8080_Diver earned 250 total points
ID: 34240118
Frankly, I would be tempted to create a SELECT query that selects the data without the AVG (and just the PlanJoin.[Plan], instead) and then apply the AVG in an outer select so that the GROUP BY is a far simpler clause.
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 1

Author Comment

by:SPLady
ID: 34243283
Thank you @8080 driver, can you show me an example of that query?
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 34243397
I think that thie following should work.  (Can't test it because I don't have the database. ;-)
SELECT  DISTINCT 

        WorkingDays

       ,salesOrderLine

       ,AVG(PlanJoin.[Plan]) AS AvgPlan

       ,EnergyOrg

       ,transyear

       ,subProduct

       ,recType

       ,Month

       ,Year

       ,lineValue

       ,transDate

FROM

(

 SELECT DATEDIFF(DD, @start, @end) + 1) - 

        (DATEDIFF(DD, DATEADD(DD, 6 - (DATEPART(DW, @start) + 

         @@DATEFIRST - 2) % 7, @start), 

         DATEADD(DD, - ((DATEPART(DW, @end) + @@DATEFIRST - 2) % 7), @end)) - 1) / 7 * 2 -

         CASE (DATEPART(DW, @start) + @@DATEFIRST - 2) % 7 + 1 

              WHEN 7

              THEN 1

              ELSE 2

         END -

         CASE (DATEPART(DW, @end) + @@DATEFIRST - 2) % 7 + 1 WHEN 6

              THEN 1                                       WHEN 7

              THEN 2

              ELSE 0

         END AS WorkingDays,

       vReportData.salesOrderLine,

       PlanJoin.[Plan],

       vReportData.EnergyOrg,

       vReportData.transyear,

       vReportData.subProduct,

       PlanJoin.recType,

       PlanJoin.Month,

       PlanJoin.Year,

       vReportData.lineValue,

       vReportData.transDate

FROM   vReportData

INNER JOIN PlanJoin

ON     vReportData.recType = PlanJoin.recType

       AND vReportData.EnergyOrg = PlanJoin.EnergyOrg

       AND vReportData.transyear = PlanJoin.Year

       AND MONTH(vReportData.transDate) = PlanJoin.Month

WHERE vReportData.transDate > '2010-11-15 00:00:00'

) Z

GROUP BY WorkingDays,

       salesOrderLine,

       EnergyOrg,

       transyear,

       subProduct,

       recType,

       Month,

       Year,

       lineValue,

       transDate;

Open in new window

0
 
LVL 1

Author Comment

by:SPLady
ID: 34243522
what is an outer reference?
0
 
LVL 22

Assisted Solution

by:8080_Diver
8080_Diver earned 250 total points
ID: 34243941
I am pretty sure that it has something to do with the case statement in you GROUP BY clause.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now