SQL Group by error - “不是GROUP BY表达式”

时间:2012-10-12 17:08:20

标签: sql oracle

我的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)

数据库架构:

客户:

enter image description here

订单行&订单:

enter image description here

我想要实现的目标: 我正在尝试按书名进行分组,以便它不显示重复的标题。

如果我错过任何事情,我道歉,谢谢。

3 个答案:

答案 0 :(得分:14)

你不明白什么?您的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,并在distinct子句中使用select

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

答案 1 :(得分:0)

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而不是吗?

答案 2 :(得分:0)

听起来你想要的是一个客户列表和他们购买的独特书籍。我会像这样重写这个查询,它应该做你想做的事情:

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曾经有一个非标准的实现,允许这种古怪的东西。它在这种情况下所做的是基本上将组聚合成一行,然后在组中所有行的每一行中聚合聚合。不完全是直观的行为。