1

下面是一个 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
4

3 回答 3

2

因为,正如它所说,如果您使用的是 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          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 
于 2012-08-31T13:57:02.353 回答
1

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

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

例如

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
于 2012-08-31T13:58:27.593 回答
1

由于您是按 PRODID 分组的,因此在选择列表中应该只存在 PRODID,如果您想要选择列表中的任何其他列,则应该使用聚合函数,如 AVG()、MIN() MAX() 等。

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

select distinct * from table

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

分组示例

于 2012-08-31T13:58:59.323 回答