SQL:聚合函数或GROUP BY

时间:2012-08-31 13:54:51

标签: sql sql-server

下面是一个sql语句和我收到的错误。我已经从两个表中包含了所有可能的字段,出了什么问题?我想通过prodID

对所有返回的项目进行分组

列'orders.adminStatus'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中

 SELECT TOP 20 
         ADMINSTATUS,   CURRENCY,   CUSTOMERID,     CUSTOMERMESSAGE,    DELTIME,    DELTOTAL,   DISCOUNT,   DISCOUNTCODE,   
         IP,    ITEMDES,    OPTIONS,    ORDEREDID,  ORDERTOTAL,
         oi.ORDER_ID,   o.ORDER_ID,     PAYMENTTYPE,    PPRICE,     PRODID,     QTY,    REASON,     SECURITYKEY,    SHIPADDRESS1,   
         SHIPADDRESS2,  SHIPADDRESS3,   SHIPCITY,   SHIPCOMPANY,    
         SHIPCOUNTRY,   SHIPCOUNTY,     SHIPMETHOD,
         SHIPNAME,  SHIPPOSTCODE,   STATUS,     STOCKCODE,  SUBTOTAL,   TAX,    TAXABLE,    TIMEDATE,   TXAUTHNO,   VENDORTXCODE,   VPSTXID
         FROM orderedItems oi
         left join orders o on  oi.order_id = o.order_id


    Group by PRODID

3 个答案:

答案 0 :(得分:2)

正如它所说的那样,如果您使用GROUP BY,则需要按要返回的字段进行分组,或者对其应用聚合函数;例如:MAX,MIN等

SELECT TOP 20  
         ADMINSTATUS,   CURRENCY,   CUSTOMERID,     CUSTOMERMESSAGE,    DELTIME,    DELTOTAL,   DISCOUNT,   DISCOUNTCODE,    
         IP,    ITEMDES,    OPTIONS,    ORDEREDID,  ORDERTOTAL, 
         oi.ORDER_ID,   o.ORDER_ID,     PAYMENTTYPE,    PPRICE,     PRODID,     QTY,    REASON,     SECURITYKEY,    SHIPADDRESS1,    
         SHIPADDRESS2,  SHIPADDRESS3,   SHIPCITY,   SHIPCOMPANY,     
         SHIPCOUNTRY,   SHIPCOUNTY,     SHIPMETHOD, 
         SHIPNAME,  SHIPPOSTCODE,   STATUS,     STOCKCODE,  SUBTOTAL,   TAX,    TAXABLE,    TIMEDATE,   TXAUTHNO,   VENDORTXCODE,   VPSTXID 
         FROM orderedItems oi 
         left join orders o on  oi.order_id = o.order_id 



Group by          ADMINSTATUS,   CURRENCY,   CUSTOMERID,     CUSTOMERMESSAGE,    DELTIME,    DELTOTAL,   DISCOUNT,   DISCOUNTCODE,    
     IP,    ITEMDES,    OPTIONS,    ORDEREDID,  ORDERTOTAL, 
     oi.ORDER_ID,   o.ORDER_ID,     PAYMENTTYPE,    PPRICE,     PRODID,     QTY,    REASON,     SECURITYKEY,    SHIPADDRESS1,    
     SHIPADDRESS2,  SHIPADDRESS3,   SHIPCITY,   SHIPCOMPANY,     
     SHIPCOUNTRY,   SHIPCOUNTY,     SHIPMETHOD, 
     SHIPNAME,  SHIPPOSTCODE,   STATUS,     STOCKCODE,  SUBTOTAL,   TAX,    TAXABLE,    TIMEDATE,   TXAUTHNO,   VENDORTXCODE,   VPSTXID 

答案 1 :(得分:1)

如果要选择单独的列,则它们都需要出现在GROUP BY子句中,除非它们是聚合函数。

见这里:http://msdn.microsoft.com/en-us/library/ms177673.aspx

e.g。

SELECT TOP 20 
         ADMINSTATUS,   CURRENCY,   CUSTOMERID,     CUSTOMERMESSAGE,    DELTIME,    DELTOTAL,   DISCOUNT,   DISCOUNTCODE,   
         IP,    ITEMDES,    OPTIONS,    ORDEREDID,  ORDERTOTAL,
         oi.ORDER_ID,   o.ORDER_ID,     PAYMENTTYPE,    PPRICE,     PRODID,     QTY,    REASON,     SECURITYKEY,    SHIPADDRESS1,   
         SHIPADDRESS2,  SHIPADDRESS3,   SHIPCITY,   SHIPCOMPANY,    
         SHIPCOUNTRY,   SHIPCOUNTY,     SHIPMETHOD,
         SHIPNAME,  SHIPPOSTCODE,   STATUS,     STOCKCODE,  SUBTOTAL,   TAX,    TAXABLE,    TIMEDATE,   TXAUTHNO,   VENDORTXCODE,   VPSTXID
         FROM orderedItems oi
         left join orders o on  oi.order_id = o.order_id


    Group by ADMINSTATUS,   CURRENCY,   CUSTOMERID,     CUSTOMERMESSAGE,    DELTIME,    DELTOTAL,   DISCOUNT,   DISCOUNTCODE,   
         IP,    ITEMDES,    OPTIONS,    ORDEREDID,  ORDERTOTAL,
         oi.ORDER_ID,   o.ORDER_ID,     PAYMENTTYPE,    PPRICE,     PRODID,     QTY,    REASON,     SECURITYKEY,    SHIPADDRESS1,   
         SHIPADDRESS2,  SHIPADDRESS3,   SHIPCITY,   SHIPCOMPANY,    
         SHIPCOUNTRY,   SHIPCOUNTY,     SHIPMETHOD,
         SHIPNAME,  SHIPPOSTCODE,   STATUS,     STOCKCODE,  SUBTOTAL,   TAX,    TAXABLE,    TIMEDATE,   TXAUTHNO,   VENDORTXCODE,   VPSTXID

答案 2 :(得分:1)

由于您按PRODID进行分组,因此在选择列表中只应存在PRODID,如果您想要选择列表中的任何其他列,则应使用AVG(),MIN()MAX()等汇总结果给出它。 ..

如果按表中可用的所有列进行分组,则等于

select distinct * from table

通常GROUP BY与聚合函数一起使用来获取表中每个组的MIN,MAX等

GROUP BY examples