您好,只是学习SQL Server
这给了我一个错误:
Incorrect syntax near ')'.'
查询:
SELECT p1.categoryid
,p1.productname
,p1.unitprice
FROM production.products AS p1
CROSS APPLY (
SELECT min(p2.unitprice)
FROM production.products AS p2
WHERE p1.categoryid = p2.categoryid
)
答案 0 :(得分:3)
您仍需要表别名并定义列:
select p1.categoryid, p1.productname, p1.unitprice
from production.products p1 cross apply
(select min(p2.unitprice) as minunitprice
from production.products p2
where p1.categoryid = p2.categoryid
) p2;
您没有从子查询中选择任何内容。我假设你打算:
select p1.categoryid, p1.productname, p1.unitprice, p2.minunitprice
from production.products p1 cross apply
(select min(p2.unitprice) as minunitprice
from production.products p2
where p1.categoryid = p2.categoryid
) p2;
如果是这样,您可以将其写为:
select p.*, min(p.unitprice) over (partition by p.categoryid)
from production.products p;
答案 1 :(得分:1)
交叉应用块之后,您应该添加别名。
SELECT p1.categoryid
,p1.productname
,p1.unitprice
,X.min_unitPrice
FROM products AS p1
CROSS APPLY (
SELECT min(unitprice) min_unitPrice
FROM products p2
WHERE p1.categoryid = p2.categoryid
) X
答案 2 :(得分:0)
您需要为子查询列指定别名,如下所示:
min(p2.unitprice) unitprice