我在这个查询中使用的SQL如下:
SELECT p.publisherID, p.publisherName, month(o.orderDate), sum(ol.quantity)
FROM Orders o, orderLine ol, Publisher p, Book b
WHERE ol.orderNum = o.orderNum
And ol.isbn is not null
And b.isbn=ol.isbn
And p.publisherID=b.publisherID
Group By p.publisherID, p.publisherName;我得到了这个错误:
Msg 8120, Level 16, State 1, Line 1
Column 'Orders.orderDate' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.现在您可以看到,orderDate列实际上在一个函数中,但是为什么我仍然会得到这个错误?我能想到的惟一一件事是,当我将鼠标悬停在month()函数上时,它会显示(表达式datetime),而orderDate只是一个"date“数据类型。我不知道这是不是问题所在。
发布于 2017-02-16 00:03:09
SELECT p.publisherID, p.publisherName, month(o.orderDate), sum(ol.quantity)
FROM Orders o, orderLine ol, Publisher p, Book b
WHERE ol.orderNum = o.orderNum
And ol.isbn is not null
And b.isbn=ol.isbn
And p.publisherID=b.publisherID
Group By p.publisherID, p.publisherName, month(o.orderDate);这将返回
+-----+------+-------+-------+
| 1 |Bookco|January| 12 |
| 1 |Bookco|Febuary| 6 |
| 2 |NextBk|January| 2 |
+-----+------+-------+-------+如果没有组中的订单日期,编译器就不知道如何处理每个出版商的各种月份。
发布于 2017-02-17 12:40:44
Group BY子句的一些规则:
项
- Expressions in Group BY
- Aggregate Functions (SUM, COUNT, MAX, Min, AVG)
group by子句中的
中
正如其他人所解释的,Month不是一个聚合函数,而是一个标量函数。
发布于 2017-02-16 00:00:53
正如错误所述,您必须在Group By列表中添加Orders.orderDate。
https://stackoverflow.com/questions/42254074
复制相似问题