Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I am doing one project in that i have to find the total number of customer order
i write the query following



SQL
SELECT tbl_customer.*, tbl_order.*, tbl_order_status.*, tbl_item.* ,count(tbl_order.order_id)
FROM tbl_order INNER JOIN tbl_item ON tbl_order.item_id = tbl_item.item_id INNER JOIN tbl_order_status ON tbl_order.order_id = tbl_order_status.order_id INNER JOIN tbl_customer ON tbl_order.customer_id = tbl_customer.customerid
  group by tbl_customer.customerid   

but when I run that query I got following eror


It does not contain aggregate function or group by clause

Plz provide me solustionas fast as possible because I have to submit the project


provide me possible if example
Posted
Updated 31-Dec-13 15:15pm
v5
Comments
Zoltán Zörgő 31-Dec-13 15:25pm    
Tag mismatch. You should add sql instead of html.

The other way around it is, use a CTE to group a unique identifier and the data being grouped, then join it in.

SQL
with grouping as 
(
  select customer_id, count(order_id) as orderCount from tbl_order group by customer_id 
)


SELECT tbl_customer.*, tbl_order.*, tbl_order_status.*, tbl_item.* , grouping.orderCount
FROM tbl_order INNER JOIN
tbl_item ON tbl_order.item_id = tbl_item.item_id INNER JOIN
tbl_order_status ON tbl_order.order_id = tbl_order_status.order_id INNER JOIN
tbl_customer ON tbl_order.customer_id = tbl_customer.customerid
inner join grouping on tbl_order.order_id = grouping.order_id
group by tbl_customer.customerid 


My signature block links to my articles on SQL, the third is on how CTEs work.
 
Share this answer
 
Usig grouping requires following a simple rule: what's not in aggregate, has to be in group by clause. So if you use so many table.* in the projection part you need to include all hose fields in the group by clause. Which makes no much sense. So I suggest to restrict the number of fields displayed, and include those that are not in an aggregation (every one not in the count).
 
Share this answer
 
v2

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