SQL分组依据错误-“;不是按表达式分组;

SQL分组依据错误-“;不是按表达式分组;,sql,oracle,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 ord

我的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 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 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
子句中的列。试试这个:

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 

也许可以使用DISTINCT

听起来你想要的是一份客户名单和他们购买的不同书籍。我会像这样重写查询,它应该满足您的要求:

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子句中的派生表(也称为内联视图或虚拟表)更改为使用
GROUPBY
,而不是
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\u address
的语义是什么?您已按客户id对订单进行分组,并将整个组折叠为一行
customer\u id
很容易,因为这是分组标准:根据定义,整个组共享相同的值<代码>计数(*)也很简单,因为它只是组中的行计数

“shipping_address”是一个问题:该组可能有多个shipping address值,但聚合组只能返回一个值。第一?最后?还有别的吗


SQL Server过去有一个非常不标准的实现,允许这种奇怪的东西。在本例中,它所做的是将组聚合为一行,然后跨组中的所有行将聚合的行连接起来。不完全是直觉行为。

你的答案非常有效。一个问题,只有当我选择与select中相同的值时,group by才会起作用吗?在标准SQL和大多数数据库引擎中,不属于聚合函数的所有列(如SUM、AVG、MIN、MAX)都需要位于
group by
子句中。但在查询中选择的任何列上都没有Agg函数。那为什么要用
分组?我不明白。@Annjawn。在这种情况下,
groupby
distinct
具有相同的效果。是的,我认为他想使用
disect
<如果在所选的任何列中使用了聚合函数,则使用“代码>分组依据”
。检查select语句中的所有列是否都在group by中…查询中没有聚合函数。那为什么要用
分组?