选择列表,因为它不包含在聚合函数或GROUP BY子句中

时间:2013-12-07 07:11:43

标签: sql sql-server tsql

我的查询会在执行时抛出此错误 - 如何解决此问题?

  

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

这是查询

SELECT 
   master_order.order_id, master_order.order_no, 
   master_order.program_no, master_order.package_type,
   article_production.article_code, article_production.weight,
   article_production.gsm, color.color_name, color.color_no,
   size.size_name, transaction_order.quantity, transaction_production.avrg, 
   transaction_production.total_weight, transaction_order.piece_carton, 
   transaction_order.no_of_carton, transaction_order.unit_name, 
   transaction_order.packs, transaction_order.kdnr,
   master_order.order_date, inlay.inlay_name, yarn.yarn_count,
   buyer.buyer_code, master_production.shipment_date,
   master_order.confirmation_date, master_order.comments, 
   master_production.carton_label, carton.carton_size, carton.carton_value,
   master_order.special_instruction, master_order.ean_code1,
   master_order.ean_code2, master_order.ean_code3, master_order.ean_code4, 
   article_production.article_name, transaction_order.piece_weight,
   article_production.machine_size, article_production.guage, 
   master_production.image_path, transaction_production.m3, 
   transaction_order.tabpage_no, transaction_order.serial_sort,
   master_order.total_tabpage, 

当我添加此错误时显示

  sum(distinct transaction_production.m3) as tm3

继续使用现有代码......

from 
    transaction_order 
LEFT JOIN 
    article_order on transaction_order.article_id = article_order.article_id
LEFT JOIN 
    size on transaction_order.size_id = size.size_id
LEFT JOIN 
    color on transaction_order.color_id = color.color_id
INNER JOIN 
    master_order ON transaction_order.order_id = master_order.order_id
LEFT JOIN 
    buyer ON master_order.buyer_id = buyer.buyer_id 
INNER JOIN 
    master_production ON master_order.order_id = master_production.order_id
LEFT JOIN 
    transaction_production ON transaction_order.trans_id = transaction_production.trans_id
LEFT JOIN 
    article_production on transaction_production.article_id = article_production.article_id
LEFT JOIN 
    inlay on master_production.inlay_id = inlay.inlay_id
LEFT JOIN 
    carton ON transaction_production.carton_id = carton.carton_id
LEFT JOIN 
    yarn ON transaction_production.yarn_id = yarn.yarn_id
WHERE
    master_order.program_no = '13-101117'

1 个答案:

答案 0 :(得分:0)

只要使用sum(...),一个聚合函数,就需要使用GROUP BY

此外,您的SQL代码中包含HTML。

... 
sum(distinct transaction_production.m3) as tm3 <p>when i add this error shown</p>
...

造成你的问题