【问题标题】:Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. - error当不使用 EXISTS 引入子查询时,选择列表中只能指定一个表达式。 - 错误
【发布时间】:2015-06-29 21:42:53
【问题描述】:

我对第二个问题有疑问,因为我收到错误“当子查询没有与 EXISTS 一起引入时,选择列表中只能指定一个表达式。”。你能帮帮我吗?

Select P.Name, P.ProductCategoryID, PC.Name, Max(P.ListPrice) as MaxPrice
from SalesLT.Product as P
join SalesLT.ProductCategory as PC
on PC.ProductCategoryID=P.ProductCategoryID
where P.ListPrice=
    (select Max(ListPrice)
    from SalesLT.Product as P2
    where P2.ProductCategoryID=P.ProductCategoryID)
group by P.Name, P.ProductCategoryID, PC.Name
order by MaxPrice desc;

go

with sale_cte as 
    (Select PC.Name, P.ProductCategoryID, P.Listprice,
    ROW_NUMBER() over(partition by PC.Name order by P.Listprice) as RN
    from SalesLT.Product as P
    join SalesLT.ProductCategory as PC
    on PC.ProductCategoryID=P.ProductCategoryID)
Select PC.Name,
    (select *
    from sale_cte)
from SalesLT.ProductCategory as PC
wher RN=1

【问题讨论】:

  • 投反对票原因:阅读错误信息。

标签: sql sql-server


【解决方案1】:

我认为错误在以下部分

Select PC.Name,
    (select *
    from sale_cte)
from SalesLT.ProductCategory as PC
where RN=1

这可以重写为

select * from sale_cte
wher RN=1

【讨论】:

    【解决方案2】:

    您不需要单独列出 PC.Name,因为它是整个列列表的一部分 (*)

    【讨论】:

    • 但我想在第二个中查看产品类别名称(与第一个 quary 中的结果相同)。
    猜你喜欢
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 2011-09-24
    • 2018-04-14
    相关资源
    最近更新 更多