SQL不是单组组函数计算

时间:2016-05-27 07:19:04

标签: sql oracle greatest-n-per-group ora-00937

Select CATEGORY, TITLE, RETAIL
From BOOKS
Where RETAIL = ( SELECT RETAIL-max(COST) From Books)
ORDER BY Category ASC;

我想要达到的目的是显示零售价低于所有书籍的最低成本

实施例

MAX COST = $44
Display number of retail items that are LESS than $44 

2 个答案:

答案 0 :(得分:0)

CREATE TABLE TBL_BOOKS_PRICE
(
BOOK VARCHAR(24),
PRICE MONEY
)
GO

INSERT INTO TBL_BOOKS_PRICE
VALUES('BOOK1',33.00)
GO

INSERT INTO TBL_BOOKS_PRICE
VALUES('BOOK1',44.00)
GO

INSERT INTO TBL_BOOKS_PRICE
VALUES('BOOK1',43.00)
GO

INSERT INTO TBL_BOOKS_PRICE
VALUES('BOOK1',38.00)
GO

SELECT BOOK,PRICE FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY PRICE DESC) ID,* FROM TBL_BOOKS_PRICE
) A
WHERE ID<>1;

答案 1 :(得分:0)

试试这个..

with tmp as (
select category, title, retail, max(cost) as retail_max_cost
from books group by category, title, retail)

select category, title, retail from tmp
where retail < retail_max_cost order by category;