GROUP BY语句不起作用

时间:2013-11-04 05:50:39

标签: sql

这是我的SQL Server查询。当我执行此查询时,Group by给了我一个错误

  

Msg 8120,Level 16,State 1,Line 1
  列'master_order.order_id'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

我的表格有关但他没有找到为什么如果不能通过其工作使用组

SELECT     
   master_order.order_id, master_order.order_no, article_order.article_name, 
   size.size_name, transaction_order.quantity, transaction_order.unit_name, 
   transaction_order.rate, transaction_order.amount, transaction_order.discount, 
   transaction_order.net_amount, master_order.order_date, buyer.buyer_name, 
   master_order.shipment_date, bank.bank_name, bank.bank_branch, bank.account_title, 
   master_order.confirmation_date, payment.payment_terms, agent.agent_name, 
   agent.company_name, currency.currency_symbol, master_order.half_day, 
   master_order.half_date, master_order.shipped_board_date, master_order.port_category, 
   port_info_setup.port_name, port_info_setup.country_name, master_order.revised, 
   master_order.confirmed, master_order.ex_factory, master_order.comments, 
   master_order.dis_type
FROM 
   master_order 
INNER JOIN
   transaction_order ON master_order.order_id = transaction_order.order_id 
INNER JOIN
   size ON transaction_order.size_id = size.size_id 
INNER JOIN
   article_order ON transaction_order.article_id = article_order.article_id 
INNER JOIN
   buyer ON master_order.buyer_id = buyer.buyer_id 
INNER JOIN
   port_info_setup ON master_order.port_id = port_info_setup.port_id 
INNER JOIN
   payment ON master_order.payment_id = payment.payment_id 
INNER JOIN
   currency ON master_order.currency_id = currency.currency_id 
INNER JOIN
   bank ON master_order.bank_id = bank.bank_id AND buyer.buyer_id = bank.buyer_id 
INNER JOIN
   agent ON master_order.agent_id = agent.agent_id
GROUP BY 
   size.size_name

1 个答案:

答案 0 :(得分:0)

在分组查询中,选择列表中的列应位于聚合函数内,或者无需聚合的列应在group by语句中提及。 如果您想要一个没有聚合的列并且不希望按该列进行分组,那么请使用CTE进行分组并使用连接在查询结果中包含该列。