onebite2
asked on
Sql query error: invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I have a query written to populate 3 columns Date,Total Orders,Amount in a table.But now I want to add few more columns in the query to elaborate the table with 6 more columns.But When I try to add column in the SELECT LIST it is throwing an error:
"invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
Here goes my query:
I want to add columns like FirstName, lastname, shipping from the orders table.
Thanks!
"invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
Here goes my query:
I want to add columns like FirstName, lastname, shipping from the orders table.
Thanks!
fUNCTION:
create function dbo.CreateDateList(@start datetime, @end datetime) returns @t table ( [date] datetime ) as begin
if @start is null or @end is null
return
if @start > @end
return
set @start = convert(datetime, convert(varchar(10), @start, 120), 120)
set @end = convert(datetime, convert(varchar(10), @end, 120), 120)
while @start < @end
begin
insert into @t ( [date] ) values (@start)
set @start = dateadd(day, 1, @start)
end
return
end
---QUERY
SELECT Convert(Varchar(15), l.[date],101)as Date,COUNT(o.OrderID ) AS TotalOrders,
ISNULL(Round(SUM(o.SubTotal),2),0) AS Amount , 1 as OrderByCol
FROM dbo.CreateDateList( @DateFrom , @DateTo ) l
LEFT OUTER JOIN orders o ON o.Datecompleted >=Convert(Datetime, l.[date],101) and o.Datecompleted < dateadd(day,1,convert(Datetime, l.[date],101))
WHERE StoreID=@StoreID
GROUP BY Convert(Datetime,l.[date],101)
Union
SELECT 'Grand Total' as Date, NULL AS TotalOrders, ISNULL(Round(SUM(o.SubTotal),2),0) AS Amount, 2 as OrderByCol
FROM dbo.CreateDateList(@DateFrom ,@DateTo ) l
LEFT OUTER JOIN orders o ON o.Datecompleted >=Convert(Datetime, l.[date],101) and o.Datecompleted < dateadd(day,1,convert(Datetime, l.[date],101))
WHERE StoreID= @StoreID
Order by Date
The above query will return the min first, last, and order. They may not even corespond to each other.
What are you trying to display?
What are you trying to display?
try this...
fUNCTION:
create function dbo.CreateDateList(@start datetime, @end datetime) returns @t table ( [date] datetime ) as begin
if @start is null or @end is null
return
if @start > @end
return
set @start = convert(datetime, convert(varchar(10), @start, 120), 120)
set @end = convert(datetime, convert(varchar(10), @end, 120), 120)
while @start < @end
begin
insert into @t ( [date] ) values (@start)
set @start = dateadd(day, 1, @start)
end
return
end
---QUERY
SELECT Convert(Varchar(15), l.[date],101) as Date,o.FirstName, o.LastNAme, o.shipping,COUNT(o.OrderID ) AS TotalOrders, ISNULL(Round(SUM(o.SubTotal),2),0) AS Amount , 1 as OrderByCol
FROM dbo.CreateDateList( @DateFrom , @DateTo ) l
LEFT OUTER JOIN orders o ON o.Datecompleted >=Convert(Datetime, l.[date],101) and o.Datecompleted < dateadd(day,1,convert(Datetime, l.[date],101))
WHERE StoreID=@StoreID
GROUP BY Convert(Datetime,l.[date],101),o.FirstName, o.LastNAme, o.shipping
Union
SELECT 'Grand Total' as Date, NULL,NULL,NULL,NULL AS TotalOrders, ISNULL(Round(SUM(o.SubTotal),2),0) AS Amount, 2 as OrderByCol
FROM dbo.CreateDateList(@DateFrom ,@DateTo ) l
LEFT OUTER JOIN orders o ON o.Datecompleted >=Convert(Datetime, l.[date],101) and o.Datecompleted < dateadd(day,1,convert(Datetime, l.[date],101))
WHERE StoreID= @StoreID
Order by Date
ASKER
I got the same error again,after making changes.
Column 'orders.FirstName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Can you tell me what could we do next?
Thanks!
Column 'orders.FirstName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Can you tell me what could we do next?
Thanks!
your GROUP BY clause before the UNION
GROUP BY Convert(Datetime,l.[date], 101) should be GROUP BY Convert(Varchar(15), l.[date],101)
try it...
GROUP BY Convert(Datetime,l.[date],
try it...
or change the MIN() function to MAX().
post the qurey you are trying to run. the one above is the original without the 3 fields you need.
ASKER
I used my function and query to display a table with Date ,Total No of orders on that date ,Total amount on that day and Grand total amount of all days .
But now I want to include additional columns like First name,last name,shipping form orders table and I am getting this error.I used the following query which u agve me.
Column 'orders.FirstName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Help me to find a work around.
Thanks!
But now I want to include additional columns like First name,last name,shipping form orders table and I am getting this error.I used the following query which u agve me.
Column 'orders.FirstName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Help me to find a work around.
Thanks!
SELECT Convert(Varchar(15), l.[date],101) as Date,o.SFirstName, o.SLastNAme, o.shipped,COUNT(o.OrderID ) AS TotalOrders, ISNULL(Round(SUM(o.SubTotal),2),0) AS Amount , 1 as OrderByCol
-- SELECT Convert(Varchar(15), l.[date],101)as Date,o.SFirstName,COUNT(o.OrderID ) AS TotalOrders, ISNULL(Round(SUM(o.SubTotal),2),0) AS Amount , 1 as OrderByCol
FROM dbo.CreateDateList( '1/21/2007' , '1/22/2008' ) l
LEFT OUTER JOIN orders o ON o.Datecompleted >=Convert(Datetime, l.[date],101) and o.Datecompleted < dateadd(day,1,convert(Datetime, l.[date],101))
WHERE StoreID='SAM'
GROUP BY Convert(Varchar(15), l.[date],101)
--GROUP BY Convert(Datetime,l.[date],101)
Union
SELECT 'Grand Total' as Date,o.SFirstName, NULL AS TotalOrders, ISNULL(Round(SUM(o.SubTotal),2),0) AS Amount, 2 as OrderByCol
FROM dbo.CreateDateList('1/21/2007' , '1/22/2008' ) l
LEFT OUTER JOIN orders o ON o.Datecompleted >=Convert(Datetime, l.[date],101) and o.Datecompleted < dateadd(day,1,convert(Datetime, l.[date],101))
WHERE StoreID='SAM'
Order by Date
need to add it to the group by.
SELECT Convert(Varchar(15), l.[date],101) as Date,o.SFirstName, o.SLastNAme, o.shipped,COUNT(o.OrderID ) AS TotalOrders, ISNULL(Round(SUM(o.SubTotal),2),0) AS Amount , 1 as OrderByCol
-- SELECT Convert(Varchar(15), l.[date],101)as Date,o.SFirstName,COUNT(o.OrderID ) AS TotalOrders, ISNULL(Round(SUM(o.SubTotal),2),0) AS Amount , 1 as OrderByCol
FROM dbo.CreateDateList( '1/21/2007' , '1/22/2008' ) l
LEFT OUTER JOIN orders o ON o.Datecompleted >=Convert(Datetime, l.[date],101) and o.Datecompleted < dateadd(day,1,convert(Datetime, l.[date],101))
WHERE StoreID='SAM'
GROUP BY Convert(Varchar(15), l.[date],101),o.SFirstName, o.SLastNAme, o.shipped
--GROUP BY Convert(Datetime,l.[date],101)
Union
SELECT 'Grand Total' as Date,o.SFirstName, NULL AS TotalOrders, ISNULL(Round(SUM(o.SubTotal),2),0) AS Amount, 2 as OrderByCol
FROM dbo.CreateDateList('1/21/2007' , '1/22/2008' ) l
LEFT OUTER JOIN orders o ON o.Datecompleted >=Convert(Datetime, l.[date],101) and o.Datecompleted < dateadd(day,1,convert(Datetime, l.[date],101))
WHERE StoreID='SAM'
Order by Date
ASKER
I did the same thing added it to the GroupBy ...what you posted but no luck its still showing the same error.......
Can we find any other work around?
Thanks!
Can we find any other work around?
Thanks!
are you using this code or manually changing another file to have the same changes? can you try running this.
do not make changes just run this query, can be in it's own window.
do not make changes just run this query, can be in it's own window.
SELECT Convert(Varchar(15), l.[date],101) as Date,o.SFirstName, o.SLastNAme, o.shipped,COUNT(o.OrderID ) AS TotalOrders, ISNULL(Round(SUM(o.SubTotal),2),0) AS Amount , 1 as OrderByCol
FROM dbo.CreateDateList( '1/21/2007' , '1/22/2008' ) l
LEFT OUTER JOIN orders o ON o.Datecompleted >=Convert(Datetime, l.[date],101) and o.Datecompleted < dateadd(day,1,convert(Datetime, l.[date],101))
WHERE StoreID='SAM'
GROUP BY Convert(Varchar(15), l.[date],101) as Date,o.SFirstName, o.SLastNAme, o.shipped
sorry, you will need to replace the sql at the bottom b/c the function is on the same script.
ASKER
The first part of the sql is running and giving me correct output.....its giving me all the columns.......but when the second part is attached its throwing errors like that.
Second part is added to get the GrandTotal for the total amount field at the bottom....what can we do so that even this works?Do you have any work around?
I appreciate ur help!
Second part is added to get the GrandTotal for the total amount field at the bottom....what can we do so that even this works?Do you have any work around?
I appreciate ur help!
SELECT Convert(Varchar(15), l.[date],101) as Date,o.SFirstName, o.SLastNAme, o.shipped,COUNT(o.OrderID ) AS TotalOrders, ISNULL(Round(SUM(o.SubTotal),2),0) AS Amount , 1 as OrderByCol
-- SELECT Convert(Varchar(15), l.[date],101)as Date,o.SFirstName,COUNT(o.OrderID ) AS TotalOrders, ISNULL(Round(SUM(o.SubTotal),2),0) AS Amount , 1 as OrderByCol
FROM dbo.CreateDateList( '1/21/2007' , '1/22/2008' ) l
LEFT OUTER JOIN orders o ON o.Datecompleted >=Convert(Datetime, l.[date],101) and o.Datecompleted < dateadd(day,1,convert(Datetime, l.[date],101))
WHERE StoreID='sam'
GROUP BY Convert(Varchar(15), l.[date],101),o.SFirstName, o.SLastNAme, o.shipped
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hey mfsamuel,
I could get the problem solved ...Thanks for ur help I appreciate!
Cindy
I could get the problem solved ...Thanks for ur help I appreciate!
Cindy
Open in new window