Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 相关子查询的内部查询中group by出错_Sql_Sql Server_Sql Server 2012_Group By_Correlated Subquery - Fatal编程技术网

Sql 相关子查询的内部查询中group by出错

Sql 相关子查询的内部查询中group by出错,sql,sql-server,sql-server-2012,group-by,correlated-subquery,Sql,Sql Server,Sql Server 2012,Group By,Correlated Subquery,我编写了以下查询,该查询返回的输出没有任何错误消息,但我发现输出有问题: select productid, productname, categoryid, unitprice FROM production.products as PP where unitprice in (select min(unitprice) as minprice from production.products as PC gro

我编写了以下查询,该查询返回的输出没有任何错误消息,但我发现输出有问题:

select productid, productname, categoryid, unitprice
FROM production.products as PP
where unitprice in (select min(unitprice) as minprice 
                    from production.products as PC
                    group by categoryid)
order  by categoryid
go
结果:

24  Product QOGNU   1   4.50
3   Product IMEHJ   2   10.00
19  Product XKXDO   3   9.20
21  Product VJZZH   3   10.00
33  Product ASTMN   4   2.50
52  Product QSRXF   5   7.00
54  Product QAQRL   6   7.45
74  Product BKAZJ   7   10.00
13  Product POXFU   8   6.00
输出显示categoryid=3的多行。当我们按类别分组时,它不应该只显示每个类别的一行(一分钟单价)吗

我哪里做错了?
提前感谢大家的帮助

当前查询的问题是,最低价格的子查询实际上是从每个类别获取的所有最低价格的集合。但您确实希望将查询限制为每个类别的最低价格。一种方法是加入子查询,以您想要的方式限制结果集

SELECT
    PP.productid,
    PP.productname,
    PP.categoryid,
    PP.unitprice
FROM production.products AS PP
INNER JOIN
(
    SELECT categoryid, MIN(unitprice) AS minprice 
    FROM production.products
    GROUP BY categoryid
) t
    ON PP.categoryid = t.categoryid AND
       PP.unitprice  = t.minprice
ORDER BY categoryid

您的查询不相关。你似乎打算:

select productid, productname, categoryid, unitprice
FROM production.products  p
where p.unitprice = (select min(p2.unitprice) as minprice 
                     from production.products p2
                     where p2.categoryid = p.categoryid
                    )
order by p.categoryid;
分组依据
不会生成相关子查询。需要一个
,其中
(好的,有时也需要
打开)

您的特定查询存在逻辑问题。它可以得到任何产品,其价格是任何类别的最低价格——甚至不是它自己的

我将这样写:

select p.productid, p.productname, p.categoryid, p.unitprice
from (select p.*,
             min(p.price) over (partition by p.categoryid) as minprice
      from production.products p
     ) p
where p.price = p.minprice
order by p.categoryid;
注意:如果多个产品都有相同的最低价格,则返回所有产品。如果您特别想要一个,请使用
行号()


是否要获取每个类别的最低单价

 SELECT * FROM (
     SELECT productid, productname, categoryid, unitprice,ROW_NUMBER()OVER(PARTITION BY categoryid ORDER BY unitprice) AS ln
     FROM production.products as PP
 ) AS t WHERE t.ln=1
order  by categoryid

您仅在子查询中分组。此外,您在
中的
还可以与每个价格进行比较,而不是与产品类别中的价格进行比较。查看Tim答案,了解如何使用
加入
谢谢大家。现在我知道我错在哪里了。你的回答很有帮助!
 SELECT * FROM (
     SELECT productid, productname, categoryid, unitprice,ROW_NUMBER()OVER(PARTITION BY categoryid ORDER BY unitprice) AS ln
     FROM production.products as PP
 ) AS t WHERE t.ln=1
order  by categoryid