Click here to Skip to main content
15,887,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table having three columns named OrderId,Seller,Date. I want to fetch data from table using group by seller but it works only when i write query as

SQL
select seller from tablename where OrderId=6 Group By Seller


but it gives error when i write query as follows

SQL
select *from tablename when<big></big> OrderId=6 Group By Seller


Error: tablename.OrderId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

How to resolve it. Is there any other way to fetch data without repeatation of same row
Posted
Updated 5-Jan-14 20:42pm
v2
Comments
Amir Mahfoozi 6-Jan-14 2:52am    
It seems that it is a UI problem and not a SQL one. Just get plain data ordered by seller and group them in UI (whether report or grid).

When you want to group rows together you have to tell SQL how to group them.
Using * (bad habit anyway) tells SQL to bring all the fields but tells nothing about how to group them over different rows...
http://technet.microsoft.com/en-us/library/ms177673(v=sql.100).aspx[^]
 
Share this answer
 
Hi..,

On using GroupBy, you must mention the column names that exists in the select query.

Include OrderId also in GroupBy caluse.
 
Share this answer
 
ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
8 Muffy 24 Indore 10000.00


SELECT * FROM CUSTOMERS
where AGE in
(SELECT AGE
FROM CUSTOMERS
GROUP BY AGE
HAVING COUNT(AGE) >= 2)
order by ID
 
Share this answer
 
Comments
Richard Deeming 11-Sep-20 6:38am    
An unformatted, unexplained, and unrelated code-dump is not a "solution" to this question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900