2013-01-09 146 views

回答

0

如果你只是想知道Originator_Acc_ID一個以上的交易,那麼你可以使用ROW_NUMBER:

select Originator_Acc_ID, Transaction_Amount 
from 
(
    select Originator_Acc_ID, Transaction_Amount, 
    row_number() over(partition by Originator_Acc_ID order by Originator_Acc_ID) rn 
    from yourtable 
    --where Transaction_Amount > 100000 
) src 
where rn > 1 

SQL Fiddle with demo

如果他們有row_number()大於一個就意味着他們有更多的比一筆交易。以上將返回所有那些不止一個。如果你只想要一個以上交易帳戶的列表,那麼你可以使用:

select distinct Originator_Acc_ID 
from 
(
    select Originator_Acc_ID, Transaction_Amount, 
    row_number() over(partition by Originator_Acc_ID order by Originator_Acc_ID) rn 
    from yourtable 
    --where Transaction_Amount > 100000 
) src 
where rn > 1 

甚至:

select Originator_Acc_ID 
from 
(
    select Originator_Acc_ID, Transaction_Amount, 
    row_number() over(partition by Originator_Acc_ID order by Originator_Acc_ID) rn 
    from yourtable 
    --where Transaction_Amount > 100000 
) src 
where rn = 2 
0

有可能是一個更好的辦法,但是這似乎工作;

;WITH MyTable AS 
(
    SELECT Originator_Acc_ID = '001' 
      ,Transaction_Amount = 10000000 
    UNION ALL 
    SELECT '001', 20000000 
    UNION ALL 
    SELECT '003', 1000 
    UNION ALL 
    SELECT '002', 60000000 
) 
,CTE AS 
(
    SELECT Originator_Acc_ID 
      ,Transaction_Amount 
      ,TxnPerOrig = COUNT(*) OVER (PARTITION BY Originator_Acc_ID) 

    FROM MyTable 
) 
SELECT DISTINCT Originator_Acc_ID 
FROM CTE 
WHERE Transaction_Amount > 100000 
AND TxnPerOrig > 1