 |
|
 |
|
Next: login - trusted connection
|
| Author |
Message |
External

Since: Mar 04, 2007 Posts: 13
|
(Msg. 1) Posted: Thu Oct 05, 2006 12:01 am
Post subject: Column X is invalid in the select list ...not contained in either an aggregate function or the GROUP BY clause Archived from groups: microsoft>public>sqlserver>programming (more info?)
|
|
|
I'm having a problem with a select query. I want to return a GrandTotal
value grouped by name, but the query analyzer throws an error:
Column 'ct.Total' is invalid in the select list because it is
not contained in either an aggregate function or the GROUP BY clause.
If I add that column to the group by clause, the results won't group
properly. Is there a workaround or is this just a bad query?
/*
Mock Query
*/
SELECT c.Name, ISNULL(ct.Total, 0) + SUM(ISNULL(divt.Total, 0)) AS
GrandTotal
FROM @Customer c
LEFT JOIN @Invoice ct ON c.Customer = ct.Customer
LEFT JOIN @Invoice divt ON c.Customer = divt.ParentCustomer
GROUP BY c.Name
ORDER BY c.Name
/*
Sample mock/data
*/
DECLARE @Customer Table (
Customer int,
Name varchar(50),
ParentCustomer int
)
DECLARE @Invoice Table (
Invoice int,
Customer int,
ParentCustomer int,
Total money
)
INSERT INTO @Customer (Customer, Name, ParentCustomer) VALUES (1, 'Customer
A', 0)
INSERT INTO @Customer (Customer, Name, ParentCustomer) VALUES (2, 'Customer
A: Division 1', 1)
INSERT INTO @Customer (Customer, Name, ParentCustomer) VALUES (3, 'Customer
A: Division 2', 1)
INSERT INTO @Customer (Customer, Name, ParentCustomer) VALUES (4, 'Customer
B', 0)
INSERT INTO @Customer (Customer, Name, ParentCustomer) VALUES (5, 'Customer
C', 0)
INSERT INTO @Customer (Customer, Name, ParentCustomer) VALUES (6, 'Customer
C: Division 1', 5)
INSERT INTO @Invoice (Invoice, Customer, ParentCustomer, Total) VALUES (1,
1, 0, 50.00)
INSERT INTO @Invoice (Invoice, Customer, ParentCustomer, Total) VALUES (2,
1, 0, 50.00)
INSERT INTO @Invoice (Invoice, Customer, ParentCustomer, Total) VALUES (3,
2, 1, 25.00)
INSERT INTO @Invoice (Invoice, Customer, ParentCustomer, Total) VALUES (4,
2, 1, 30.50)
INSERT INTO @Invoice (Invoice, Customer, ParentCustomer, Total) VALUES (5,
3, 1, 45.00)
INSERT INTO @Invoice (Invoice, Customer, ParentCustomer, Total) VALUES (6,
4, 0, 75.00)
INSERT INTO @Invoice (Invoice, Customer, ParentCustomer, Total) VALUES (7,
6, 5, 50.00) >> Stay informed about: Column X is invalid in the select list ...not contained in.. |
|
| Back to top |
|
 |  |
External

Since: Jan 10, 2008 Posts: 463
|
(Msg. 2) Posted: Thu Oct 05, 2006 12:49 am
Post subject: Re: Column X is invalid in the select list ...not contained in either an aggregate function or the GROUP BY clause [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Thanks for providing the DDL and sample data. The only thing missing was
the result you wanted from your sample data. I'm guessing that the result
you want is:
Name Total
GrandTotal
-------------------------------------------------- --------------------- ---------------------
Customer A 100.0000
200.5000
Customer A: Division 1 55.5000
55.5000
Customer A: Division 2 45.0000
45.0000
Customer B 75.0000
75.0000
Customer C .0000
50.0000
Customer C: Division 1 50.0000
50.0000
If so, then use the following:
SELECT x.Name,
SUM(CASE WHEN x.Source = 1 THEN Coalesce(x.Total,0) ELSE 0 END) As Total,
SUM(Coalesce(x.Total,0)) AS GrandTotal
FROM (SELECT c.Name, ct.Total, 1 As Source
FROM @Customer c
LEFT JOIN @Invoice ct ON c.Customer = ct.Customer
UNION ALL
SELECT c.Name, divt.Total, 2 As Source
FROM @Customer c
LEFT JOIN @Invoice divt ON c.Customer = divt.ParentCustomer) AS x
GROUP BY x.Name
ORDER BY x.Name;
Tom
"Janet" wrote in message
> I'm having a problem with a select query. I want to return a GrandTotal
> value grouped by name, but the query analyzer throws an error:
>
> Column 'ct.Total' is invalid in the select list because it is
> not contained in either an aggregate function or the GROUP BY
> clause.
>
> If I add that column to the group by clause, the results won't group
> properly. Is there a workaround or is this just a bad query?
>
> /*
> Mock Query
> */
> SELECT c.Name, ISNULL(ct.Total, 0) + SUM(ISNULL(divt.Total, 0)) AS
> GrandTotal
> FROM @Customer c
> LEFT JOIN @Invoice ct ON c.Customer = ct.Customer
> LEFT JOIN @Invoice divt ON c.Customer = divt.ParentCustomer
> GROUP BY c.Name
> ORDER BY c.Name
>
>
> /*
> Sample mock/data
> */
>
> DECLARE @Customer Table (
> Customer int,
> Name varchar(50),
> ParentCustomer int
> )
>
> DECLARE @Invoice Table (
> Invoice int,
> Customer int,
> ParentCustomer int,
> Total money
> )
>
> INSERT INTO @Customer (Customer, Name, ParentCustomer) VALUES (1,
> 'Customer
> A', 0)
> INSERT INTO @Customer (Customer, Name, ParentCustomer) VALUES (2,
> 'Customer
> A: Division 1', 1)
> INSERT INTO @Customer (Customer, Name, ParentCustomer) VALUES (3,
> 'Customer
> A: Division 2', 1)
> INSERT INTO @Customer (Customer, Name, ParentCustomer) VALUES (4,
> 'Customer
> B', 0)
> INSERT INTO @Customer (Customer, Name, ParentCustomer) VALUES (5,
> 'Customer
> C', 0)
> INSERT INTO @Customer (Customer, Name, ParentCustomer) VALUES (6,
> 'Customer
> C: Division 1', 5)
>
> INSERT INTO @Invoice (Invoice, Customer, ParentCustomer, Total) VALUES (1,
> 1, 0, 50.00)
> INSERT INTO @Invoice (Invoice, Customer, ParentCustomer, Total) VALUES (2,
> 1, 0, 50.00)
> INSERT INTO @Invoice (Invoice, Customer, ParentCustomer, Total) VALUES (3,
> 2, 1, 25.00)
> INSERT INTO @Invoice (Invoice, Customer, ParentCustomer, Total) VALUES (4,
> 2, 1, 30.50)
> INSERT INTO @Invoice (Invoice, Customer, ParentCustomer, Total) VALUES (5,
> 3, 1, 45.00)
> INSERT INTO @Invoice (Invoice, Customer, ParentCustomer, Total) VALUES (6,
> 4, 0, 75.00)
> INSERT INTO @Invoice (Invoice, Customer, ParentCustomer, Total) VALUES (7,
> 6, 5, 50.00)
>
>
> >> Stay informed about: Column X is invalid in the select list ...not contained in.. |
|
| Back to top |
|
 |  |
External

Since: Mar 04, 2007 Posts: 13
|
(Msg. 3) Posted: Thu Oct 05, 2006 1:09 am
Post subject: Re: Column X is invalid in the select list ...not contained in either an aggregate function or the GROUP BY clause [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
External

Since: Sep 29, 2006 Posts: 239
|
(Msg. 4) Posted: Mon Dec 11, 2006 2:11 pm
Post subject: Re: Column X is invalid in the select list ...not contained in either an aggregate function or the GROUP BY clause [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Gert-Jan Strik wrote:
> glenn wrote:
> >
> > Hi Tom or anyone else out here ,
> >
> > I have a similar problem and was wondering if you provide some advice.
> >
> > The following SQL executes properly.
> >
> > DECLARE @IntVar As int
> >
> > SELECT @IntVar = 965
> >
> > SELECT POS.IssueNumber,
> > DATEPART(ww,CAST(POS.SaleDate As DateTime)) AS Week,
> > -- dbo.svf_GetFirstWeek(@IntVar,POS.IssueNumber,CAST(DATEPART(ww,CAST(POS.SaleDate As DateTime)) As Int)),
> > SUM(Quantity) As QuantitySold
> > FROM dbo.PointOfSale POS
> > WHERE POS.IntVar = @IntVar
> > GROUP BY POS.IssueNumber,DATEPART(ww,CAST(POS.SaleDate As DateTime))
> > ORDER BY POS.IssueNumber,DATEPART(ww,CAST(POS.SaleDate As DateTime))
> >
> > Notice the function call is commented out.
> >
> > If I uncomment the function call then I get the following error:
> >
> > Msg 8120, Level 16, State 1, Line 5
> > Column 'POS.SaleDate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
> >
> > Any suggestions? Your help is greatly appreciated.
> >
> > Regards,
> > Glenn
> >
> > From http://www.developmentnow.com/g/113_2006_10_0_0_830990/Column-X-is-inv...d-in-th
> >
> > Posted via DevelopmentNow.com Groups
> > http://www.developmentnow.com
>
> The function dbo.svf_GetFirstWeek is not an aggregate. This basically
> means, that if there are two rows with the same POS.SaleDate, then there
> would be two "GetFirstWeek" values, which is invalid.
>
> If you know the function will always return the same value for each
> "DATEPART(ww,CAST(POS.SaleDate As DateTime))", then you can wrap the
> expression in a MAX aggregate (or another aggregate). For example:
>
> SELECT POS.IssueNumber,
> DATEPART(ww,CAST(POS.SaleDate As DateTime)) AS Week,
>
> MAX(dbo.svf_GetFirstWeek(@IntVar,POS.IssueNumber,CAST(DATEPART(ww,CAST(POS.SaleDate
> As DateTime)) As Int))),
> SUM(Quantity) As QuantitySold
> FROM dbo.PointOfSale POS
> WHERE POS.IntVar = @IntVar
> GROUP BY POS.IssueNumber, DATEPART(ww,CAST(POS.SaleDate As DateTime))
> ORDER BY IssueNumber, Week
>
>
> HTH,
> Gert-Jan
Hi Gert-Jan ,
If I knew the function will always return the same value for each
"DATEPART(ww,CAST(POS.SaleDate As DateTime))",
I would rather make as little UDF calls as possible, as follows:
SELECT IssueNumber, Week,
dbo.svf_GetFirstWeek(@IntVar,POS.IssueNumber,week),
QuantitySold
FROM(
SELECT POS.IssueNumber,
DATEPART(ww,CAST(POS.SaleDate As DateTime)) AS Week,
SUM(Quantity) As QuantitySold
FROM dbo.PointOfSale POS
WHERE POS.IntVar = @IntVar
GROUP BY POS.IssueNumber,DATEPART(ww,CAST(POS.SaleDate As DateTime))
ORDER BY POS.IssueNumber,DATEPART(ww,CAST(POS.SaleDate As DateTime))
) t
Might perform much better - UDF calls are not cheap.
What do you think?
-----------------------
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/ >> Stay informed about: Column X is invalid in the select list ...not contained in.. |
|
| Back to top |
|
 |  |
External

Since: Dec 11, 2006 Posts: 1
|
(Msg. 5) Posted: Mon Dec 11, 2006 3:48 pm
Post subject: RE: Column X is invalid in the select list ...not contained in either an aggregate function or the GROUP BY clause [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Hi Tom or anyone else out here  ,
I have a similar problem and was wondering if you provide some advice.
The following SQL executes properly.
DECLARE @IntVar As int
SELECT @IntVar = 965
SELECT POS.IssueNumber,
DATEPART(ww,CAST(POS.SaleDate As DateTime)) AS Week,
-- dbo.svf_GetFirstWeek(@IntVar,POS.IssueNumber,CAST(DATEPART(ww,CAST(POS.SaleDate As DateTime)) As Int)),
SUM(Quantity) As QuantitySold
FROM dbo.PointOfSale POS
WHERE POS.IntVar = @IntVar
GROUP BY POS.IssueNumber,DATEPART(ww,CAST(POS.SaleDate As DateTime))
ORDER BY POS.IssueNumber,DATEPART(ww,CAST(POS.SaleDate As DateTime))
Notice the function call is commented out.
If I uncomment the function call then I get the following error:
Msg 8120, Level 16, State 1, Line 5
Column 'POS.SaleDate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Any suggestions? Your help is greatly appreciated.
Regards,
Glenn
From http://www.developmentnow.com/g/113_2006_10_0_0_830990/Column-X-is-inv...d-in-th
Posted via DevelopmentNow.com Groups
http://www.developmentnow.com >> Stay informed about: Column X is invalid in the select list ...not contained in.. |
|
| Back to top |
|
 |  |
External

Since: Jan 10, 2008 Posts: 463
|
(Msg. 6) Posted: Mon Dec 11, 2006 3:48 pm
Post subject: Re: Column X is invalid in the select list ...not contained in either an aggregate function or the GROUP BY clause [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Hi Glenn,
As far as I can see, your syntax ought to be perfectly valid (because you
are not directly using SaleDate, but rather the DATEPART... and that is in
your GROUP BY clause). But I have seen the Query Processor make this kind
of error before. What I usually do is use a derived table in my query so
that I generate the value I want to GROUP BY on in a simple column and then
everything goes well. In your case something like:
DECLARE @IntVar As int
SELECT @IntVar = 965
SELECT x.IssueNumber,
x.Week,
dbo.svf_GetFirstWeek(@IntVar,x.IssueNumber,x.Week),
SUM(x.Quantity) As QuantitySold
FROM (SELECT POS.IssueNumber,
DATEPART(ww,CAST(POS.SaleDate As DateTime)) AS Week,
Quantity
FROM dbo.PointOfSale POS
WHERE POS.IntVar = @IntVar) x
GROUP BY x.IssueNumber, x.Week
ORDER BY x.IssueNumber, x.Week
Doing this keeps the Query Processor from getting confused (and has the
added advantage that you only have to type " DATEPART(ww,CAST(POS.SaleDate
As DateTime))" one time rather than four times <grin>.
Tom
"glenn" wrote in message
> Hi Tom or anyone else out here ,
>
> I have a similar problem and was wondering if you provide some advice.
>
> The following SQL executes properly.
>
> DECLARE @IntVar As int
>
> SELECT @IntVar = 965
>
> SELECT POS.IssueNumber,
> DATEPART(ww,CAST(POS.SaleDate As DateTime)) AS Week,
> --
> dbo.svf_GetFirstWeek(@IntVar,POS.IssueNumber,CAST(DATEPART(ww,CAST(POS.SaleDate
> As DateTime)) As Int)),
> SUM(Quantity) As QuantitySold
> FROM dbo.PointOfSale POS
> WHERE POS.IntVar = @IntVar
> GROUP BY POS.IssueNumber,DATEPART(ww,CAST(POS.SaleDate As DateTime))
> ORDER BY POS.IssueNumber,DATEPART(ww,CAST(POS.SaleDate As DateTime))
>
> Notice the function call is commented out.
>
> If I uncomment the function call then I get the following error:
>
> Msg 8120, Level 16, State 1, Line 5
> Column 'POS.SaleDate' is invalid in the select list because it is not
> contained in either an aggregate function or the GROUP BY clause.
>
> Any suggestions? Your help is greatly appreciated.
>
> Regards,
> Glenn
>
> From
> http://www.developmentnow.com/g/113_2006_10_0_0_830990/Column-X-is-inv...d-in-th
>
> Posted via DevelopmentNow.com Groups
> http://www.developmentnow.com >> Stay informed about: Column X is invalid in the select list ...not contained in.. |
|
| Back to top |
|
 |  |
External

Since: Sep 09, 2003 Posts: 255
|
(Msg. 7) Posted: Mon Dec 11, 2006 9:37 pm
Post subject: Re: Column X is invalid in the select list ...not contained in either an [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
glenn wrote:
>
> Hi Tom or anyone else out here ,
>
> I have a similar problem and was wondering if you provide some advice.
>
> The following SQL executes properly.
>
> DECLARE @IntVar As int
>
> SELECT @IntVar = 965
>
> SELECT POS.IssueNumber,
> DATEPART(ww,CAST(POS.SaleDate As DateTime)) AS Week,
> -- dbo.svf_GetFirstWeek(@IntVar,POS.IssueNumber,CAST(DATEPART(ww,CAST(POS.SaleDate As DateTime)) As Int)),
> SUM(Quantity) As QuantitySold
> FROM dbo.PointOfSale POS
> WHERE POS.IntVar = @IntVar
> GROUP BY POS.IssueNumber,DATEPART(ww,CAST(POS.SaleDate As DateTime))
> ORDER BY POS.IssueNumber,DATEPART(ww,CAST(POS.SaleDate As DateTime))
>
> Notice the function call is commented out.
>
> If I uncomment the function call then I get the following error:
>
> Msg 8120, Level 16, State 1, Line 5
> Column 'POS.SaleDate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
>
> Any suggestions? Your help is greatly appreciated.
>
> Regards,
> Glenn
>
> From http://www.developmentnow.com/g/113_2006_10_0_0_830990/Column-X-is-inv...d-in-th
>
> Posted via DevelopmentNow.com Groups
> http://www.developmentnow.com
The function dbo.svf_GetFirstWeek is not an aggregate. This basically
means, that if there are two rows with the same POS.SaleDate, then there
would be two "GetFirstWeek" values, which is invalid.
If you know the function will always return the same value for each
"DATEPART(ww,CAST(POS.SaleDate As DateTime))", then you can wrap the
expression in a MAX aggregate (or another aggregate). For example:
SELECT POS.IssueNumber,
DATEPART(ww,CAST(POS.SaleDate As DateTime)) AS Week,
MAX(dbo.svf_GetFirstWeek(@IntVar,POS.IssueNumber,CAST(DATEPART(ww,CAST(POS.SaleDate
As DateTime)) As Int))),
SUM(Quantity) As QuantitySold
FROM dbo.PointOfSale POS
WHERE POS.IntVar = @IntVar
GROUP BY POS.IssueNumber, DATEPART(ww,CAST(POS.SaleDate As DateTime))
ORDER BY IssueNumber, Week
HTH,
Gert-Jan >> Stay informed about: Column X is invalid in the select list ...not contained in.. |
|
| Back to top |
|
 |  |
External

Since: Sep 09, 2003 Posts: 255
|
(Msg. 8) Posted: Tue Dec 12, 2006 10:06 pm
Post subject: Re: Column X is invalid in the select list ...not contained in either an [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Alex Kuznetsov wrote:
>
> Gert-Jan Strik wrote:
> > The function dbo.svf_GetFirstWeek is not an aggregate. This basically
> > means, that if there are two rows with the same POS.SaleDate, then there
> > would be two "GetFirstWeek" values, which is invalid.
> >
> > If you know the function will always return the same value for each
> > "DATEPART(ww,CAST(POS.SaleDate As DateTime))", then you can wrap the
> > expression in a MAX aggregate (or another aggregate). For example:
> >
> > SELECT POS.IssueNumber,
> > DATEPART(ww,CAST(POS.SaleDate As DateTime)) AS Week,
> >
> > MAX(dbo.svf_GetFirstWeek(@IntVar,POS.IssueNumber,CAST(DATEPART(ww,CAST(POS.SaleDate
> > As DateTime)) As Int))),
> > SUM(Quantity) As QuantitySold
> > FROM dbo.PointOfSale POS
> > WHERE POS.IntVar = @IntVar
> > GROUP BY POS.IssueNumber, DATEPART(ww,CAST(POS.SaleDate As DateTime))
> > ORDER BY IssueNumber, Week
> >
> >
> > HTH,
> > Gert-Jan
>
> Hi Gert-Jan ,
>
> If I knew the function will always return the same value for each
> "DATEPART(ww,CAST(POS.SaleDate As DateTime))",
> I would rather make as little UDF calls as possible, as follows:
>
> SELECT IssueNumber, Week,
> dbo.svf_GetFirstWeek(@IntVar,POS.IssueNumber,week),
> QuantitySold
> FROM(
> SELECT POS.IssueNumber,
> DATEPART(ww,CAST(POS.SaleDate As DateTime)) AS Week,
> SUM(Quantity) As QuantitySold
> FROM dbo.PointOfSale POS
> WHERE POS.IntVar = @IntVar
> GROUP BY POS.IssueNumber,DATEPART(ww,CAST(POS.SaleDate As DateTime))
> ORDER BY POS.IssueNumber,DATEPART(ww,CAST(POS.SaleDate As DateTime))
> ) t
>
> Might perform much better - UDF calls are not cheap.
> What do you think?
SQL Server 2000 does not cache any UDF results. I don't think SQL Server
2005 does either, but I have not experimented a lot with this on 2005.
Whether rewriting the query is more efficient depends on the cost of the
UDF call and the rest of the query. If there are many rows and few
duplicate "DATEPART(ww,CAST(POS.SaleDate As DateTime))", then the cost
of storing and re-processing the temporary result (caused by the derived
table) might exceed the benefit of fewer UDF executions. However, if the
UDF is expensive, then in most real life scenario's it will save some or
even a considerable amount of time. So again, the standard answer
applies: it depends!
If the query performance is satisfactory, I would not rewrite the query.
First of all, the query becomes less readable / more complex. Also, a
feature release or service pack could implement UDF result caching.
Gert-Jan >> Stay informed about: Column X is invalid in the select list ...not contained in.. |
|
| Back to top |
|
 |  |
| Related Topics: | Error: is invalid in the select list because it is not con.. - Hi, I have a stored procedure and when I execute it, I always get the following error: "Column 'DistributionPrintReceiptInstance.DistributionCommonInstanceID' is invalid in the select list because it is not contained in either an aggregate functio...
column alias defined in select list and used in same selec.. - I want to define a column alias and then I want to use it in the same SELECT statement. I know it can't be done... is there a workaround? This is the Select statement that I'd like to reuse. Someone suggested a derived table and/or a view. This SELEC...
Invalid Column Error - Hi, Can anybody point me why I get the error "Invalid Column Name - emp" error. Thanks. Table structures are: Tree create table tree (emp Char(10) Not Null, Boss Char(10)); Stack CREATE TABLE Stack (Stack_top INTEGER Not Null, emp Char(1...
Invalid column name error missing? - Hi all, I have the following T-SQL script: USE tempdb BEGIN TRAN CREATE TABLE TABLE1(id int, ref int) CREATE TABLE TABLE2(id int) SELECT id FROM TABLE1 WHERE ref IN (SELECT ref FROM TABLE2) DROP TABLE TABLE1 DROP TABLE TABLE2 COMMIT TRAN GO I..
Invalid dates in datetime column - I have bad values in a datetime column. dbcc ran with no errors. The first things that come to mind are a hardware problems or memory corruption. Does anyone have any ideas on how it could have happened? SQL Server 2000 SP3 Clustered Windows 2000.. |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|