目标:显示图书的ID,标题和酒吧。销售额最高的图书年份;包括关系。在确定图书销售时使用总延期成本(数量*订单价格)。
规则:
查询:
SELECT book_id,
title,
year_publd
FROM bkinfo.books
where book_id =
(
SELECT TOP 1 WITH TIES book_id, quantity*order_price as Extended_cost
from bkorders.order_details
order by quantity*order_price DESC
)
;
错误:
Msg 116,Level 16,State 1,Line 10只有一个表达式 在未引入子查询时在选择列表中指定 存在。
子查询显示的book_id数量最多但是当它作为一个整体运行时,我收到一个错误。请解释一下我应该做些什么。
答案 0 :(得分:2)
;with cte as
(
select book_id, quantity*order_price as Extended_cost from bkorders.order_details
order by Extended_cost DESC
)
select top 1 with ties * from cte
答案 1 :(得分:1)
SELECT book_id, title, year_publd
FROM bkinfo.books
where book_id =
(
SELECT TOP 1 book_id
from bkorders.order_details
order by quantity*order_price DESC
)
;