SiteExperts.com Logo Home | Community | Developer's Paradise
User Groups | Site Tools | Site Information | Search
 Main Menu
 Forums
SiteExperts.com Forums
All Discussions

SiteExperts Feedback
The Lounge
Dynamic HTML
Site Design/ Critiques
HTML and CSS
XML Technologies
The Wireless Internet
Internet Explorer
Microsoft .NET
The Server
Technical Support

Sponsored Links

User Groups : Forums : SiteExperts : The Server :

Previous DiscussionNext Discussion
 SQL COUNT() Help

Hi ,

i have two tables (users,orders) , i need to select all fields of users + total orders that user made !

i used this query :

select users.*,count(order_id) as [ordersTotal] from _users Left Join _orders on _users.user_id = _orders.order_user order by user_name

but it says something abbout fields that are not in group by clause.

what is wrong here ? should i add all fields to group by ?

Started By vahid_reza on May 21, 2007 at 5:18:50 AM

7 Response(s) | Reply

Earlier Replies | Replies 1 to 7 of 7 | Later Replies
Goto Page: 1
bod1467 on May 21, 2007 at 5:48:27 AM (# 1)
This message has been edited.

Try this ...

SELECT COUNT(orders.order_id) AS ordersTotal, users.*
FROM users
INNER JOIN orders on users.user_id = orders.order_user
GROUP BY users.user_name
ORDER BY users.user_name


vahid_reza on May 21, 2007 at 6:20:45 AM (# 2)

Hi ,

Same error ! :(

it says that some fields in select list are not set in group by fields clause !


brian on May 21, 2007 at 6:32:49 AM (# 3)

2 ways.

1: list all the fields from users and include them in the group by clause

2. use a sub-query. e.g.

select users.*,(select count(orders.order_id) from orders where orders.user_id=users.user_id) as counted
from users

this will work, but can be flaky with the likes of mysql (might be ok in newer versions) - it will definitely work with ms access and sql server

 


vahid_reza on May 21, 2007 at 2:55:45 PM (# 4)

Hi

i did way 2 before post here ! in two words (IT SUCKS) . too slow ! 

and way 1 , there is about 12 fields , and more important thing , does it affect on result data ?


ChrisRickard on May 21, 2007 at 9:27:24 PM (# 5)

What is your DBMS (Oracle, MSSQL, MySQL, etc)?


brian on May 22, 2007 at 1:30:26 AM (# 6)

I use  the second technique all the time and it is just fine. It can be slow if there are loads of records to count but as long as your indexes are optimised you shouldn't really notice it.

The first method doesn't affect results but it would probably also be slow as well if you have speed issues with the second one.

I'm assuming that you only wish to pull back users with orders...

 


ja928 on Jun 20, 2007 at 12:48:53 PM (# 7)

A different twist on the subquery:

SELECT usr.*, ordersTotal
FROM_users usr
  LEFT OUTER JOIN (SELECT order_user, COUNT(_orders.order_id) as ordersTotal FROM _orders GROUP BY order_user) ord on _users.user_id = ord.order_user
ORDER BY user_name

Also, Can you add a WHERE clause to limit the number of records you're counting in _orders?


Earlier Replies | Replies 1 to 7 of 7 | Later Replies
Goto Page: 1

To respond to a discussion, you must first logon.

If you are not registered, please register yourself to become a member of the SiteExperts.community.

User Name
Password
Copyright 1997-2004 InsideDHTML.com, LLC. All rights reserved.