Solved

What does this error message mean?

Posted on 2010-11-30
7
275 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PK numbers not follow 8 61
SQL Server; storing data in offline mode. 10 68
Following an example - removing duplicate strings 4 51
How can I get this column in my query? 2 40
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

11 Experts available now in Live!

Get 1:1 Help Now