使用内连接选择多个MAX值

时间:2009-08-31 12:09:10

标签: sql sql-server

只有当StakeValue中的值不重复时,我才能查询。 基本上,我需要从SI_STAKES表中选择最大值,以及它们与按内部类型分组的其他两个表的关系。

SELECT a.StakeValue, b.[StakeName], c.[ProviderName] 
FROM SI_STAKES AS a 
INNER JOIN SI_STAKESTYPES AS b ON a.[StakeTypeID] = b.[ID] 
INNER JOIN SI_PROVIDERS AS c ON a.[ProviderID] = c.[ID] WHERE a.[EventID]=6 
  AND a.[StakeGroupTypeID]=1 
AND a.StakeValue IN 
  (SELECT MAX(d.StakeValue) FROM SI_STAKES AS d 
   WHERE d.[EventID]=a.[EventID] AND d.[StakeGroupTypeID]=a.[StakeGroupTypeID] 
   GROUP BY d.[StakeTypeID])
ORDER BY b.[StakeName], a.[StakeValue] DESC

结果例如必须是:

[ID]  [MaxValue] [StakeTypeID] [ProviderName]
1     1,5        6             provider1
2     3,75       7             provider2
3     7,6        8             provider3

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

您可以使用over子句,因为您正在使用T-SQL(希望是2005 +):

select distinct
    a.stakevalue,
    max(a.stakevalue) over (partition by a.staketypeid) as maxvalue,
    b.staketypeid,
    c.providername
from
    si_stakes a
    inner join si_stakestypes b on
        a.staketypeid = b.id
    inner join si_providers c on
        a.providerid = c.id
where
    a.eventid = 6
    and a.stakegrouptypeid = 1

基本上,这会找到每个a.stakevalue的最大a.staketypeid。使用distinct将返回一行且仅返回一行。现在,如果您想将min a.id包括在内,可以使用row_number来完成此操作:

select
    s.id,
    s.maxvalue,
    s.staketypeid,
    s.providername
from (
    select
        row_number() over (order by a.stakevalue desc 
                           partition by a.staketypeid) as rownum,
        a.id,
        a.stakevalue as maxvalue,
        b.staketypeid,
        c.providername
    from
        si_stakes a
        inner join si_stakestypes b on
            a.staketypeid = b.id
        inner join si_providers c on
            a.providerid = c.id
    where
        a.eventid = 6
        and a.stakegrouptypeid = 1
    ) s
where
    s.rownum = 1

答案 1 :(得分:2)

这里要解决两个问题。

1)查找每种类型的最大值。这将获得每个StakeType的Max值,并确保我们仅对所需事件和组类型进行练习。

SELECT StakeGroupTypeID, EventID, StakeTypeID, MAX(StakeValue) AS MaxStakeValue 
FROM   SI_STAKES 
WHERE  Stake.[EventID]=6 
AND    Stake.[StakeGroupTypeID]=1 
GROUP BY StakeGroupTypeID, EventID, StakeTypeID 

2)然后我们只需返回一个该值,因为它可能会出现一次以上。

使用最大值,我们必须为每个人找到一个唯一的行我通常通过获取Max ID来获得最新条目的附加优势。

SELECT MAX(SMaxID.ID) AS ID 
FROM   SI_STAKES AS SMaxID 
       INNER JOIN ( 
            SELECT StakeGroupTypeID, EventID, StakeTypeID, MAX(StakeValue) AS MaxStakeValue 
            FROM   SI_STAKES 
            WHERE  Stake.[EventID]=6 
            AND    Stake.[StakeGroupTypeID]=1 
            GROUP BY StakeGroupTypeID, EventID, StakeTypeID 
       ) AS SMaxVal ON SMaxID.StakeTypeID = SMaxVal.StakeTypeID 
                       AND SMaxID.StakeValue = SMaxVal.MaxStakeValue 
                       AND SMaxID.EventID = SMaxVal.EventID 
                       AND SMaxID.StakeGroupTypeID = SMaxVal.StakeGroupTypeID 

3)现在我们已经拥有了我们想要的行的ID,我们可以获得这些信息。

SELECT Stakes.ID, Stakes.StakeValue, SType.StakeName, SProv.ProviderName 
FROM   SI_STAKES AS Stakes 
       INNER JOIN SI_STAKESTYPES AS SType ON Stake.[StakeTypeID] = SType.[ID] 
       INNER JOIN SI_PROVIDERS AS SProv ON Stake.[ProviderID] = SProv.[ID] 
WHERE  Stake.ID IN (
            SELECT MAX(SMaxID.ID) AS ID 
            FROM   SI_STAKES AS SMaxID 
                   INNER JOIN ( 
                        SELECT StakeGroupTypeID, EventID, StakeTypeID, MAX(StakeValue) AS MaxStakeValue 
                        FROM   SI_STAKES 
                        WHERE  Stake.[EventID]=6 
                        AND    Stake.[StakeGroupTypeID]=1 
                        GROUP BY StakeGroupTypeID, EventID, StakeTypeID 
                   ) AS SMaxVal ON SMaxID.StakeTypeID = SMaxVal.StakeTypeID 
                                   AND SMaxID.StakeValue = SMaxVal.MaxStakeValue 
                                   AND SMaxID.EventID = SMaxVal.EventID 
                                   AND SMaxID.StakeGroupTypeID = SMaxVal.StakeGroupTypeID 
        )