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

This is step one of a query that I need to compare data from several data sources.  This query should be giving me the sum of oil, gas, and water production for each well, grouped by Well, and the year/month of production.  Right now I'm only looking at one month, but will be expanding this to look across multiple months.

declare @FromDate date
declare @ThruDAte date
set @FromDate = '2018-01-01'
set @ThruDAte = '2018-01-31'

SELECT T.*
FROM (

SELECT E.Bolo_ID
, Year(@FromDate) as ProdYear
, Month(@FromDate) as ProdMonth
, Sum(P.Gas) as Gas
, Sum(P.Oil) as Oil
, SUM(P.Water) as Water
FROM [WHR_System_Tables].dbo.tbl_sysentities as E
INNER JOIN [WHR_System_Tables].dbo.tbl_sysProduction as P ON E.Entity_ID = P.Entity_ID
WHERE (P.docDate >= @FromDate)
AND (P.DocDate < DATEADD(day, 1, @ThruDate))
GROUP BY E.Bolo_ID, Year(@FromDate), Month(@FromDate)
) as T

Open in new window

But I'm getting the following error:

Msg 164, Level 15, State 1, Line 16
Each GROUP BY expression must contain at least one column that is not an outer reference.
LVL 52
Dale FyeOwner, Developing Solutions LLCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale FyeOwner, Developing Solutions LLCAuthor Commented:
Disregard,  This should have read:

declare @FromDate date
declare @ThruDAte date
set @FromDate = '2018-01-01'
set @ThruDAte = '2018-01-31'
SELECT T.*
FROM (
SELECT E.Bolo_ID
, Year(P.DocDate) as ProdYear
, Month(P.DocDate) as ProdMonth
, Sum(P.Gas) as Gas
, Sum(P.Oil) as Oil
, SUM(P.Water) as Water
FROM [WHR_System_Tables].dbo.tbl_sysentities as E
INNER JOIN [WHR_System_Tables].dbo.tbl_sysProduction as P ON E.Entity_ID = P.Entity_ID
WHERE (P.docDate >= @FromDate)
AND (P.DocDate < DATEADD(day, 1, @ThruDate))
GROUP BY E.Bolo_ID, Year(P.docDate), Month(P.docDate )
) as T

Open in new window

Note that the Year() and Month() functions are now referring to a date field in the dataset rather than to the variables.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale FyeOwner, Developing Solutions LLCAuthor Commented:
resolved it myself by changing parameters in the Year( ) and Month( )  functions.
Scott PletcherSenior DBACommented:
No need to separate year and month, you can leave it as a true date.  Other than that, not sure what the problem is, it should run OK:


SELECT T.*
FROM (
SELECT E.Bolo_ID
, Dateadd(Month, Datediff(Month, 0, P.DocDate), 0) as ProdYearMonth
, Sum(P.Gas) as Gas
, Sum(P.Oil) as Oil
, SUM(P.Water) as Water
FROM [WHR_System_Tables].dbo.tbl_sysentities as E
INNER JOIN [WHR_System_Tables].dbo.tbl_sysProduction as P ON E.Entity_ID = P.Entity_ID
WHERE (P.docDate >= @FromDate)
AND (P.DocDate < DATEADD(day, 1, @ThruDate))
GROUP BY E.Bolo_ID, Dateadd(Month, Datediff(Month, 0, P.DocDate), 0)
) as T
Mark WillsTopic AdvisorCommented:
And shouldnt really need a subquery
declare @FromDate date
declare @ThruDAte date
set @FromDate = '2018-01-01'
set @ThruDAte = '2018-01-31'

SELECT E.Bolo_ID
, Year(P.DocDate) as ProdYear
, Month(P.DocDate) as ProdMonth
, Sum(P.Gas) as Gas
, Sum(P.Oil) as Oil
, SUM(P.Water) as Water
FROM [WHR_System_Tables].dbo.tbl_sysentities as E
INNER JOIN [WHR_System_Tables].dbo.tbl_sysProduction as P ON E.Entity_ID = P.Entity_ID
WHERE (P.docDate >= @FromDate)
AND (P.DocDate < DATEADD(day, 1, @ThruDate))
GROUP BY E.Bolo_ID, Year(P.docDate), Month(P.docDate )

Open in new window

And Scott is right about Dateadd(Month, Datediff(Month, 0, P.DocDate), 0)
Just depends on if you need separately identifiable columns...
Dale FyeOwner, Developing Solutions LLCAuthor Commented:
Mark, the subquery is because that is just part of the query, I needed to join the results of this query to another, similar, query which captures info from another set of tables.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.