4

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

数据库架构:

顾客:

在此处输入图像描述

订单行和订单:

在此处输入图像描述

我想要实现的目标: 我正在尝试按书名分组,这样它就不会显示重复的标题。

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

4

3 回答 3

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, 并仅distinctselect子句中使用:

select distinct  customers.customer_first_name, customers.customer_last_name, orders.customer_numb, books.author_name, books.title 
于 2012-10-12T17:11:01.347 回答
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 也许?

于 2012-10-12T17:11:55.560 回答
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 曾经有一个非常不标准的实现,允许这种奇怪的东西。在这种情况下,它所做的基本上是将组聚合到单行中,然后将跨组中所有行的每一行聚合的数据连接起来。不完全是直观的行为。

于 2012-10-12T18:22:18.813 回答