Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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 将查询转换为视图或表值函数_Sql_Sql Server_Tsql - Fatal编程技术网

Sql 将查询转换为视图或表值函数

Sql 将查询转换为视图或表值函数,sql,sql-server,tsql,Sql,Sql Server,Tsql,我想得到所有产品中最便宜的产品。对于一个产品来说,这很容易,但是我不能为所有的产品动脑,我不想使用光标 select top 1 pv.VariantID from ProductVariant pv inner join (select min(price) as Price from productvariant where ProductID = @ProductID) pr on pv.Price = pr.Price where pv.P

我想得到所有产品中最便宜的产品。对于一个产品来说,这很容易,但是我不能为所有的产品动脑,我不想使用光标

select top 1
    pv.VariantID
from
    ProductVariant pv
    inner join (select min(price) as Price from productvariant where ProductID = @ProductID) pr
        on pv.Price = pr.Price
where
    pv.ProductID = @ProductID
如果这两个表具有以下结构,则此操作有效:

Product - ProductID
ProductVariant - ProductID, VariantID, Price
我知道你能做到,有人知道怎么做吗

将上述查询转换为标量函数并使用它的最佳方法是什么?因此,它将成为:

select
    ProductID
    , dbo.NewFunction(ProductID)
from
    Product
这就是最低价格。如果你想要与之配套的VariantID,那么你需要再次加入——但是每个VariantID的价格是唯一的吗

SELECT VariantID, ProductID, MinPrice
FROM ProductVariant JOIN
     (SELECT ProductID, MIN(Price) MinPrice
     FROM Product JOIN ProductVariant ON Product.ProductID = ProductVariant.ProductID
     GROUP BY ProductID) min_price
     ON ProductVariant.ProductID = min_price.ProductID AND ProductVariant.Price = min_price.MinPrice
如果每个变体的价格不是唯一的,这将无法正常工作-特别是您将获得副本。如果发生这种情况,您可以将其更改为:

SELECT MIN(VariantID) SmallestVariantID, ProductID, MinPrice
FROM ProductVariant JOIN
     (SELECT ProductID, MIN(Price) MinPrice
     FROM Product JOIN ProductVariant ON Product.ProductID = ProductVariant.ProductID
     GROUP BY ProductID) min_price
     ON ProductVariant.ProductID = min_price.ProductID AND ProductVariant.Price = min_price.MinPrice
GROUP BY ProductID, MinPrice
这将从副本中选择最小的变体ID。

类似的内容

select  pv.VariantID
from    ProductVariant pv1
join    (select min(pv2.price) as Price from productvariant pv2 where pv2.ProductID = pv.ProductID) pr
        on pv1.Price = pr.Price

这是我找到的最简单的解决方案。

如果我理解正确,您有多个产品,每个都有自己的变体,每个变体都有自己的价格

如果是这样的话,我想

select p.ProductID, min(pv.Price)
from Product
  inner join ProductVariant pv on p.ProductID = pv.ProductID
将是提供结果的最简单查询

select
    pv.ProductID
    , min(pv.VariantID)
from
    ProductVariant pv
    inner join (select min(price) as Price, ProductID from productvariant group by ProductID) pr
        on pv.ProductID = pr.ProductID
        and pr.Price = pv.Price
group by
    pv.ProductID
select p.ProductID, min(pv.Price)
from Product
  inner join ProductVariant pv on p.ProductID = pv.ProductID