-3

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

消息 8120,级别 16,状态 1,行 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
4

1 回答 1

0

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

于 2013-11-04T12:25:40.517 回答