【问题标题】:SQL Group by error - "not a GROUP BY expression" [duplicate]SQL 分组错误-“不是 GROUP BY 表达式”[重复]
【发布时间】:2012-10-03 13:20:41
【问题描述】:

我的 SQL 查询似乎有问题,这让我发疯。我似乎无法让小组工作。我不断收到以下错误:

00979. 00000 -  "not a GROUP BY expression"

我的查询:

SELECT customers.customer_first_name, customers.customer_last_name, orders.customer_numb, books.author_name, books.title
FROM customers
LEFT OUTER JOIN orders ON (orders.customer_numb = customers.customer_numb)
LEFT OUTER JOIN order_lines ON (order_lines.order_numb = orders.order_numb)
LEFT OUTER JOIN books ON (books.isbn = order_lines.isbn)
WHERE (customers.customer_numb = 6)
GROUP BY (books.title)

数据库架构:

客户:

订单行和订单:

我想要达到的目标: 我正在尝试按书名分组,以免显示重复的书名。

如果我遗漏了什么,我深表歉意,谢谢。

【问题讨论】:

  • 检查select语句中的所有列是否都在group by中...
  • 您的查询中没有聚合函数。那为什么是group by

标签: sql oracle


【解决方案1】:

你不明白什么?您的 select 语句包含许多不在 group by 子句中的列。试试这个:

SELECT customers.customer_first_name, customers.customer_last_name, orders.customer_numb, books.author_name, books.title 
FROM customers 
LEFT OUTER JOIN orders ON (orders.customer_numb = customers.customer_numb) 
LEFT OUTER JOIN order_lines ON (order_lines.order_numb = orders.order_numb) 
LEFT OUTER JOIN books ON (books.isbn = order_lines.isbn) 
WHERE (customers.customer_numb = 6) 
GROUP BY customers.customer_first_name, customers.customer_last_name, orders.customer_numb, books.author_name, books.title  

由于您没有聚合任何内容,因此您可以省去group by,而只需在select 子句中使用distinct

select distinct  customers.customer_first_name, customers.customer_last_name, orders.customer_numb, books.author_name, books.title 

【讨论】:

  • 您的回答完美。一个问题,只有当我选择与 SELECT 中的值相同的值时,group by 才会起作用?
  • 在标准 SQL 和大多数数据库引擎中,所有不属于聚合函数的列(如 SUM、AVG、MIN、MAX)都需要在 group by 子句中。
  • 但是查询中选择的任何列上都没有 Agg 函数。那为什么是GROUP BY?我不明白。
  • @Annjawn 。 . .在这种情况下,group bydistinct 具有相同的效果。
【解决方案2】:
SELECT DISTINCT customers.customer_first_name, customers.customer_last_name, orders.customer_numb, books.author_name, books.title
FROM customers
LEFT OUTER JOIN orders ON (orders.customer_numb = customers.customer_numb)
LEFT OUTER JOIN order_lines ON (order_lines.order_numb = orders.order_numb)
LEFT OUTER JOIN books ON (books.isbn = order_lines.isbn)
WHERE (customers.customer_numb = 6)

改用 DISTINCT 也许?

【讨论】:

  • 是的,我想他想使用disinct。如果在任何选定的列中使用了聚合函数,则使用 group by
【解决方案3】:

听起来您想要的是一份客户列表以及他们购买的不同书籍。我会像这样重写查询,这应该可以满足您的要求:

select c.customer_first_name ,
       c.customer_last_name  ,
       c.customer_numb       ,
       b.author_name         ,
       b.title               
from customers c
left join ( select distinct
                   o.customer_numb ,
                   ol.isbn         
            from      orders      o
            left join order_lines ol on ol.order_number = orders.order_numb
          ) cb on cb.customer_numb = c.customer_numb
join books b on b.isbn = cb.isbn
where c.customer_numb = 6

如果您想计算他们购买了每个标题的数量,请将 from 子句中的派生表(也称为内联视图或虚拟表)更改为使用 group by 而不是 select distinct 并添加适当的聚合函数到结果集。

这是您的原始查询向南的地方:结果集中的每一列都必须是

  • group by 子句中的列或表达式,
  • 文字,或...
  • 聚合函数

虽然这里有一些例外情况(例如,您可以有一个仅依赖于分组列和聚合函数的表达式),虽然 SQL 标准假定允许其他列,但大多数实现都不允许。

考虑这样的查询,您的客户和订单之间存在一对多关系,其中单个订单可能会被运送到一个地址或另一个地址。

select c.customer_id , o.shipping_address , orders = count(*)
from customer c
join order    o on o.customer_id
group by c.customer_id

o.shipping_address 在这种情况下的语义是什么?您已按客户 ID 对订单进行分组,并将整个组折叠成一行。 customer_id 很简单,因为这是分组标准:根据定义,整个组共享相同的值。 count(*) 也很简单,因为它只是组中的行数。

'shipping_address' 是一个问题:该组可能有多个送货地址值,但聚合组只能返回一个值。第一的?最后的?还有什么?

SQL Server 曾经有一个非常不标准的实现,允许这种奇怪的东西。在这种情况下,它所做的基本上是将组聚合到单行中,然后将跨组中所有行的每一行聚合的数据连接起来。不完全是直观的行为。

【讨论】:

    猜你喜欢
    • 2012-12-07
    • 2014-12-28
    • 2013-04-11
    • 2013-03-19
    • 2011-08-14
    • 1970-01-01
    • 2013-03-29
    • 2013-06-25
    相关资源
    最近更新 更多