通过嵌套连接选择最高Seq数

时间:2012-04-25 17:03:13

标签: sql-server

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

此表有100000条记录。

ClientSeqTable 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

2 个答案:

答案 0 :(得分: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划分的行号分区,然后按余额递减排序。

答案 1 :(得分:0)

如果你想在MAX上使用你的内联你可以这样做

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  

DEMO

但是你应该注意到这实际上并不等同于使用row_number(mouters解决方案)。如果最大值(余额)达到平局,则每个ClID可能会返回多行。如果您需要这种处理关系的方式,并且您想使用窗口函数you could use RANK