1

我想为每个客户 ID 取最大的序列号(最大的序列号将根据最高的银行账户余额计算)。

该表有 100000 条记录。

客户端序列表 T1

ClID              SeqId     
1                 11         
1                 12         
1                 13         
1                 14         
1                 15         
2                 16         
2                 17         
2                 18         
2                 19         
3                 110         
3                 111         
3                 112         
3                 113         

SeqBranchTable T2

SeqId    BalID  
11         1  
12         2  
13         3  
14         4  
15         5  
16         6  
17         7  
18         8  
19         9  
110         10  
111         11  
112         12  
113         13

平衡表t3

BalID Balance  
1     30000  
2     26789  
3     23456  
4     12345  
5     21234  
6     12456  
7     45632  
8     23456  
9     99999  
10    12345  
11    21234  
12    12456  
13    45632

结果将是

ClID              SeqID          Balance  
1                 1              30000  
2                 9              99999  
3                 4              45632

我已经尝试过这种方式,但对我没有用

SELECT  RS.Investigationid,MAX(stt.sequenceid) 'SeqId', T.HighestBalance 'Balance' 
FROM    ClientSeqTable  T1, SeqBranchTable T2, branbaltable t3,  
                ( SELECT    t1.clid ,MAX(T3.Balance) 'HighestBalance'
                    FROM    ClientSeqTable  T1, SeqBranchTable T2, branbaltable t3,
                    WHERE   t1.seqid =  T2.seqID
                    AND     T2. balId= T3. balId
                    GROUP BY  RS.Investigationid,stt.SequenceId
                    ) T
WHERE   T2.balId =  T3.BalId
AND T1.SeqId = T2.SeqId
AND     T3.HighestBalance = T2.balance
AND     T1.clID = t.ClID
GROUP BY  T1.ClID

以上查询结果如下。

ClID            SeqNu          Bal    
1               1              30000    
1               2              30000  
1               3              30000  
1               4              30000  
1               5              30000  
2               3              99999    
2               4              99999    
2               1              99999    
2               9              99999    
3               2              45632  
3               5              45632  
3               3              45632  
3               4              45632
4

2 回答 2

5

如果您能够使用 row_number 函数,那么应该可以工作:

select 
    *
from 
(
    select 
        t1.ClID, t1.SeqId, t3.Bal, 
        RowNumber = row_number() over (PARTITION BY t1.ClID order by t3.bal desc)
    from 
        ClientSeqTable t1
    inner join
        SeqBranchTable t2 on t2.SeqId = t1.SeqId
    inner join
        Balancetable t3 on t3.BalID = t2.BalID
) t
where
    t.RowNumber = 1

重要的一点是按客户端 ID 划分行号,然后按余额降序排列。

于 2012-04-25T17:58:59.107 回答
0

如果你想在 MAX 上使用 In-line,你可以这样做

SELECT t1.ClID, 
       t1.SeqId, 
       t3.Balance 
FROM   ClientSeqTable t1 
       INNER JOIN SeqBranchTable t2 
         ON t2.SeqId = t1.SeqId 
       INNER JOIN Balancetable t3 
         ON t3.BalID = t2.BalID 
       INNER JOIN (SELECT Max(Balance) Bal, 
                          t1.ClID 
                   FROM   ClientSeqTable t1 
                          INNER JOIN SeqBranchTable t2 
                            ON t2.SeqId = t1.SeqId 
                          INNER JOIN Balancetable t3 
                            ON t3.BalID = t2.BalID 
                   GROUP  BY t1.ClID) max_bal 
         ON t1.ClID = max_bal.ClID 
            AND t3.Balance = max_bal.bal  

演示

但是您应该注意,这实际上并不等同于使用 row_number (muters解决方案)。如果 max(balance) 存在平局,则每个 ClID 可能会返回多行。如果您需要这种处理关系的方式并且想要使用窗口函数,则可以使用 RANK

于 2012-04-25T18:25:37.543 回答