473,396 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

using group by in tables joined together in sql server

20
I am newbie in sql server
i'm using sql server 2005 and the northwind database
i wanted to join order and order details tables and calculate the total purchase made by each customer by using group by clause.

When i used
Expand|Select|Wrap|Line Numbers
  1. select orders.orderid,orders.customerid,orders.orderdate,orders.shipcity,orders.shipcountry,
  2. orderdetails.unitprice,orderdetails.quantity,(orderdetails.unitprice*orderdetails.quantity )as total from orders 
  3. inner join orderdetails on orders.orderid=orderdetails.orderid group by orders.customerid
i received the error message Column 'orders.OrderID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Can anyone explain the reason for the error message?

As other way around i tried to create a view and then use the Group by clause as below
Expand|Select|Wrap|Line Numbers
  1. create view Purchase_by_each_customer
  2. as 
  3. select orders.orderid,orders.customerid,orders.orderdate,orders.shipcity,orders.shipcountry,orderdetails.unitprice,orderdetails.quantity,(orderdetails.unitprice*orderdetails.quantity )as total from orders inner join orderdetails on orders.orderid=orderdetails.orderid 
  4.  
  5. select orderid,customerid,sum(total) from Purchase_by_each_customer group by customerid
Error message is Column 'Purchase_by_each_customer.orderid' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Can aggregate functions be used with views,if so how?
how to use Group by in multiple table joins?
Mar 19 '12 #1
4 2527
NeoPa
32,556 Expert Mod 16PB
Jet SQL Help:
All fields in the SELECT field list must either be included in the GROUP BY clause or be included as arguments to an SQL aggregate function.
This quote from the Help system implies that all references to fields, even compound references, must either be aggregated (included in one of the aggregate functions - Sum(), Avg(), Min(), Max(), Count(), StDev(), Var(), First() or Last()) or included in the GROUP BY clause. Any expression which is of either type is considered aggregated. The aggregate functions can only take field reference expressions which resolve to non-aggregated fields.

Essentially, the SQL Engine needs to know how to process any particular reference. If you have a reference to a field which is not inside an Aggregate function of any type, and is also not in the GROUP BY clause, then how is the SQL Engine to know which of the many possible records in the group to take the value from.

See the situation below and ask yourself whether the result for [fB] should be "A", "B" or "C" :
Expand|Select|Wrap|Line Numbers
  1. SELECT   [fA]
  2.        , [fB]
  3.        , Sum([fC]) AS [SumC]
  4. FROM     [Tbl]
  5. GROUP BY [fA]
Expand|Select|Wrap|Line Numbers
  1. Table = [Tbl]
  2. [fA]  [fB]  [fC]
  3.  1    "A"    11
  4.  1    "B"    22
  5.  1    "C"    33
Result :
Expand|Select|Wrap|Line Numbers
  1. [fA]=1, [fB]="???", [SumC]=66
Conclusion :

The fact that you cannot determine from this information which item should be selected, is an illustration of why it doesn't makes sense unless the above criteria are met (In short that all field references are aggregated in one way or another).
Mar 19 '12 #2
NeoPa
32,556 Expert Mod 16PB
Actually, this is such a common confusion that I decided to prepare the answer as an article, which can be found at Aggregate Query Woes.
Mar 19 '12 #3
shanboy
20
Thanx,got the point all fields in select other than the aggregate fn should be included after group by clause.
Thanx a lot
Mar 21 '12 #4
NeoPa
32,556 Expert Mod 16PB
Pleased to help. You're certainly not the first to get confused by that (I beat you to it myself some while ago :-D), nor will you be the last. I just hope the article helps to steer people in the right direction in future.
Mar 22 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
4
by: Sujeet | last post by:
Hey guys, I want to import a database from SQL Server 2000 to MS Access with all indices and keys along with the tables. DTS does not export indices and keys, only the structure and the data. ...
1
by: annie | last post by:
Hi all, I have recently ported my Access 2000 app to SQL Server, keeping the Access client as the front end using linked tables. I am also using triggers on my SQL tables to trap orphan...
5
by: Christoph Sticksel | last post by:
Hi, I'm having problems with attaching two tables stored in an SQL Server 2000 to an Access 97 database. It worked well for all other tables except those two. This is what I did: Choose the...
1
by: Sarah | last post by:
Hi My tables are in vfp8 and I need to transfer data from the foxpro tables to SQL server 2000. Should I have to buy SQL Client access licences in order to fetch data from foxPro tables to SQL...
8
by: Dot Net Newbie | last post by:
New to DOTNET so please be gentle: I have an in-memory dataset that I want move to a SQL Server database. The XML schema with which the dataset was built matches exactly the table schema in SQL...
2
by: sleepyant | last post by:
Hi, I have 2 identical tables in each server that I need to compare and update/insert accordingly using SQL. For example, server A as a source and Server B as destination which both have Table1....
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
2
by: morleyc | last post by:
Hi, i've started a c# project using MS compact SQL as a datasource. I am able to add tables in the server explorer and columns in these tables, but how can i add relationships between tables...
0
by: rwschul | last post by:
I have a problem query that I could use some help. This code ran in 20 minutes last year and there is not much change in the input data this year....perhaps 10% growth. Currently this is running 8+...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.