获取与SQL Server 2008中一列的MIN和MAX值对应的其他列

时间:2013-08-16 12:27:44

标签: sql sql-server max min

我有一种情况,我需要获得列的min()和max()以及与min()和max()函数对应的另一列。我使用INNER JOIN尝试了查询,使用min()或max()时它成功了。当两者都使用时,没有任何返回。

SELECT A.IslandCode, CONVERT(DATE, A.LogTime)LogTime
, MN.Demand MinDemand, MN.TIME MinDemandAt
FROM 
(
    SELECT IslandCode, CONVERT(DATE, LogTime) LogTime, SUM(KiloWatt) Demand
    FROM ph_MachineDailyReadings 
    WHERE IslandCode = 1 AND CONVERT(DATE, LogTime) = '2013-08-13'
    GROUP BY IslandCode, LogTime
) A
INNER JOIN 
(
    SELECT TOP 1 IslandCode, CONVERT(DATE, LogTime) LogTime, SUM(KiloWatt) Demand, 
    STR(DATEPART(HOUR, LogTime)) + ':' + LTRIM(STR(DATEPART(MINUTE, LogTime))) TIME
    FROM ph_MachineDailyReadings 
    WHERE IslandCode = 1 AND CONVERT(DATE, LogTime) = '2013-08-13'
    GROUP BY IslandCode, LogTime
    ORDER BY Demand
) MN
ON A.IslandCode = MN.IslandCode AND A.LogTime = MN.LogTime AND A.Demand = MN.Demand

上述查询返回以下结果,即正确

IslandCode  LogTime     MinDemand       MinDemandAt
1           2013-08-13  698         13:0

查询max()

SELECT A.IslandCode, CONVERT(DATE, A.LogTime)LogTime
, MX.Demand MaxDemand, MX.TIME MaxDemandAt
FROM 
(
    SELECT IslandCode, CONVERT(DATE, LogTime) LogTime, SUM(KiloWatt) Demand
    FROM ph_MachineDailyReadings 
    WHERE IslandCode = 1 AND CONVERT(DATE, LogTime) = '2013-08-13'
    GROUP BY IslandCode, LogTime
) A
INNER JOIN
(
    SELECT TOP 1 IslandCode, CONVERT(DATE, LogTime) LogTime, SUM(KiloWatt) Demand, 
    STR(DATEPART(HOUR, LogTime)) + ':' + LTRIM(STR(DATEPART(MINUTE, LogTime))) TIME
    FROM ph_MachineDailyReadings 
    WHERE IslandCode = 1 AND CONVERT(DATE, LogTime) = '2013-08-13'
    GROUP BY IslandCode, LogTime
    ORDER BY Demand DESC
) MX
ON A.IslandCode = MX.IslandCode AND A.LogTime = MX.LogTime AND A.Demand = MX.Demand

返回,这也是正确的

IslandCode  LogTime       MaxDemand     MaxDemandAt
1           2013-08-13  1463            20:0

但是当两个查询都在同一个查询中使用时,不返回任何内容

SELECT A.IslandCode, CONVERT(DATE, A.LogTime)LogTime
, MX.Demand MaxDemand, MX.TIME MaxDemandAt
, MN.Demand MinDemand, MN.TIME MinDemandAt
FROM 
(
    SELECT IslandCode, CONVERT(DATE, LogTime) LogTime, SUM(KiloWatt) Demand
    FROM ph_MachineDailyReadings 
    WHERE IslandCode = 1 AND CONVERT(DATE, LogTime) = '2013-08-13'
    GROUP BY IslandCode, LogTime
) A
INNER JOIN
(
    SELECT TOP 1 IslandCode, CONVERT(DATE, LogTime) LogTime, SUM(KiloWatt) Demand, 
    STR(DATEPART(HOUR, LogTime)) + ':' + LTRIM(STR(DATEPART(MINUTE, LogTime))) TIME
    FROM ph_MachineDailyReadings 
    WHERE IslandCode = 1 AND CONVERT(DATE, LogTime) = '2013-08-13'
    GROUP BY IslandCode, LogTime
    ORDER BY Demand DESC
) MX
ON A.IslandCode = MX.IslandCode AND A.LogTime = MX.LogTime AND A.Demand = MX.Demand
INNER JOIN 
(
    SELECT TOP 1 IslandCode, CONVERT(DATE, LogTime) LogTime, SUM(KiloWatt) Demand, 
    STR(DATEPART(HOUR, LogTime)) + ':' + LTRIM(STR(DATEPART(MINUTE, LogTime))) TIME
    FROM ph_MachineDailyReadings 
    WHERE IslandCode = 1 AND CONVERT(DATE, LogTime) = '2013-08-13'
    GROUP BY IslandCode, LogTime
    ORDER BY Demand
) MN
ON A.IslandCode = MN.IslandCode AND A.LogTime = MN.LogTime AND A.Demand = MN.Demand

预期结果

IslandCode   LogTime     MinDemand  MinDemandAt   MaxDemand   MaxDemandAt
    1       2013-08-13  1463        20:0        698      13:0

我可以在单个查询中获得此结果,还是需要使用基于存储过程的解决方案?

提前致谢

2 个答案:

答案 0 :(得分:2)

您可以使用分析功能执行此操作:

WITH Data AS
(   SELECT  IslandCode, 
            LogDate = CAST(LogTime AS DATE),
            LogTime, 
            Demand = SUM(KiloWatt) ,
            RowNumAsc = ROW_NUMBER() OVER (PARTITION BY IslandCode, CAST(LogTime AS DATE) ORDER BY SUM(Kilowatt)),
            RowNumDesc = ROW_NUMBER() OVER (PARTITION BY IslandCode, CAST(LogTime AS DATE) ORDER BY SUM(Kilowatt) DESC) 
    FROM    ph_MachineDailyReadings 
    WHERE   IslandCode = 1 
    AND     CAST(LogTime AS DATE) = '20130813'
    GROUP BY IslandCode, LogTime
)
SELECT  ma.IslandCode,
        LogTime = ma.LogDate,
        MinDemand = mi.Demand,
        MinDemandAt = CAST(mi.LogTime AS TIME),
        MaxDemand = ma.Demand,
        MaxDemandAt = CAST(ma.LogTime AS TIME)
FROM    Data mi
        INNER JOIN Data ma
            ON ma.IslandCode = mi.IslandCode
            AND ma.LogDate = mi.LogDate
WHERE   mi.RowNumAsc = 1
AND     ma.RowNumDesc = 1;

<强> Example on SQL Fiddle

修改

这不需要参数,如果省略WHERE子句如下,则会在每个日期返回每个岛代码的最小值和最大值:

WITH Data AS
(   SELECT  IslandCode, 
            LogDate = CAST(LogTime AS DATE),
            LogTime, 
            Demand = SUM(KiloWatt) ,
            RowNumAsc = ROW_NUMBER() OVER (PARTITION BY IslandCode, CAST(LogTime AS DATE) ORDER BY SUM(Kilowatt)),
            RowNumDesc = ROW_NUMBER() OVER (PARTITION BY IslandCode, CAST(LogTime AS DATE) ORDER BY SUM(Kilowatt) DESC) 
    FROM    ph_MachineDailyReadings 
    GROUP BY IslandCode, LogTime
)
SELECT  ma.IslandCode,
        LogTime = ma.LogDate,
        MinDemand = mi.Demand,
        MinDemandAt = CAST(mi.LogTime AS TIME),
        MaxDemand = ma.Demand,
        MaxDemandAt = CAST(ma.LogTime AS TIME)
FROM    Data mi
        INNER JOIN Data ma
            ON ma.IslandCode = mi.IslandCode
            AND ma.LogDate = mi.LogDate
WHERE   mi.RowNumAsc = 1
AND     ma.RowNumDesc = 1;

<强> Example on SQL Fiddle

最后请注意,我已将您的logTime设置为TIME格式,因为这似乎比VARCHAR格式更有用,如果您在VARCHAR中确实需要它,您可以使用:

CONVERT(VARCHAR(10), ma.LogTime, 8)

答案 1 :(得分:0)

我认为你不需要第二次加入。您只需union all

即可完成此操作
(SELECT TOP 1 IslandCode, CONVERT(DATE, LogTime) LogTime, SUM(KiloWatt) Demand, 
        STR(DATEPART(HOUR, LogTime)) + ':' + LTRIM(STR(DATEPART(MINUTE, LogTime))) TIME
 FROM ph_MachineDailyReadings 
 WHERE IslandCode = 1 AND CONVERT(DATE, LogTime) = '2013-08-13'
 GROUP BY IslandCode, LogTime
 ORDER BY Demand
) union all
(SELECT TOP 1 IslandCode, CONVERT(DATE, LogTime) LogTime, SUM(KiloWatt) Demand, 
 STR(DATEPART(HOUR, LogTime)) + ':' + LTRIM(STR(DATEPART(MINUTE, LogTime))) TIME
 FROM ph_MachineDailyReadings 
 WHERE IslandCode = 1 AND CONVERT(DATE, LogTime) = '2013-08-13'
 GROUP BY IslandCode, LogTime
 ORDER BY Demand desc
)