T-SQL从子查询中选择MAX

时间:2016-02-12 08:47:21

标签: sql sql-server database tsql aggregate-functions

我在数据库中有以下表格。我们的产品有多个产品系列,我们有ProductVariation,每个产品有多个产品变化。

我们希望根据系列的ID在ProductVariation表上为一组产品执行一组聚合查询。对于SeriesID为276,ProductID' 400-415与SeriesID匹配。然后,我们想要在ProductVariation表中找到分配了ProductID' 400-415的各种字段的最小值和最大值。

我写的T-SQL语句如下: -

SELECT(
   SELECT MAX([X]) FROM [ProductVariation] AS B WHERE B.ProductID = A.ProductID      
)
FROM [Product] AS A
WHERE SeriesID = 12 AND IsDeleted = 0 and IsEnabled = 1

但这会返回15行最小和最大数据。我一直在寻找最大值的最大值,但我不知道如何调整上述语句来检索它。我们至少需要在同一个查询中完成10个这样的聚合。

有人可以建议如何获得最大值的最大值吗?

干杯, 麦克

6 个答案:

答案 0 :(得分:3)

基本上,你只想要整个系列的最大值?我认为在其他解决方案中有太多的MAX。只需使用联接:

SELECT MAX(PV.X), MAX(PV.Y)
FROM [Product] AS P
JOIN [ProductVariation] AS PV ON P.ProductID = PV.ProductID
WHERE P.SeriesID = 12 AND P.IsDeleted = 0 and P.IsEnabled = 1

这样,您也可以一次查询多个最大值。

答案 1 :(得分:2)

如果我理解你,你就是要求:

SELECT MAX(
   SELECT MAX([WheelDiameter]) FROM [AUT].[dbo].[Product] AS B WHERE B.ProductBlockID = A.ProductBlockID      
)
FROM [AUT].[dbo].[ProductBlock] AS A
WHERE SeriesID = 12 AND IsDeleted = 0 and IsEnabled = 1

答案 2 :(得分:1)

好吧,所以我的问题是上面的说法都没问题,但事实证明我是按照SeriesID查找组,以返回每个SeriesID的设置字段的MIN和MAX。非常感谢所有的SQL语句,你们已经帮助解决了我以前多次遇到过的问题! :)

在sql中: -

@Oliver的帖子是触发我需要的东西的原因。然后,我为GROUP BY SeriesID添加了一个group by。

在LINQ中: -

(from x in productBlocks
    join y in products on x.ProductBlockID equals y.ProductBlockID
    where x.SeriesID == id && x.IsEnabled && !x.IsDeleted
    group y by x.SeriesID into g
    select new SeriesCharacteristicsViewModel
    {
        MinWheelDiameter = g.Min(s => s.WheelDiameter),
        MaxWheelDiameter = g.Max(s => s.WheelDiameter),
        ShoreHardness = g.Select(s => s.ShoreHardness).FirstOrDefault(),
        MinimumCarryingCapacityAt4kmh = g.Min(s => s.StaticCapacity),
        MaximumCarryingCapacityAt4kmh = g.Max(s=>s.StaticCapacity),
        MinimumRollingResistance = g.Min(s=>s.RollingResistance),
        MaximumRollingResistance = g.Max(s=>s.RollingResistance),
        MinimumTemperature = g.Min(s=>s.TempFrom),
        MaximumTemperature = g.Max(s=>s.TempTo)
    }
)

答案 3 :(得分:0)

SELECT MAX(A.MAXVALUE) as MAXVALUE
FROM
(
 SELECT MAX([WheelDiameter]) MAXVALUE 
 FROM [AUT].[dbo].[Product] AS B 
 INNER JOIN FROM [AUT].[dbo].[ProductBlock] AS A 
 ON ( B.ProductBlockID = A.ProductBlockID) 
 WHERE SeriesID = 12 
 AND IsDeleted = 0 
 AND IsEnabled = 1      
)A

答案 4 :(得分:0)

试试这个:

SELECT a.SeriesID, min(c.min), max(c.max)
FROM   [Product] AS A outer apply
       ( SELECT MIN(X) min, MAX([X]) max 
         FROM   [ProductVariation] AS B 
         WHERE B.ProductID = A.ProductID ) as c
WHERE  a.SeriesID in (12,13,14) AND a.IsDeleted = 0 and a.IsEnabled = 1
GROUP BY a.SeriesID

答案 5 :(得分:0)

SELECT MAX(A.Maximum) as MAXVALUE
FROM
(
    SELECT MAX([WheelDiameter]) Maximum
    FROM [AUT].[dbo].[Product] AS Product WHERE Product.ProductBlockID = ProductBlock.ProductBlockID
    INNER JOIN [AUT].[dbo].[ProductBlock] AS ProductBlock ON Product.ProductBlockID = ProductBlock.ProductBlockID
    WHERE SeriesID = 12 AND IsDeleted = 0 and IsEnabled = 1      
) A