2010-06-25 25 views
6

從表中選擇只有一個唯一的記錄我的表有多個具有相同的MemberID的記錄。我想只得到一條記錄。如何使用唯一

select DISTINCT(MemberID) from AnnualFees; 

然後結果會來。但我想顯示其他列數據也,但當我這樣做

select DISTINCT(MemberID),StartingDate,ExpiryDate,Amount from AnnualFees; 

所有的細節,包括同樣的MemberID數據也顯示。

有人可以幫助我。

+0

那麼您應該如何選擇顯示MemberID = 123的許多AnnualFees行中的哪一個?或者你只是想要任何一個隨機排? – 2010-06-25 09:41:39

回答

18

假設你只是想任何一行隨機對每個MEMBERID比你可以這樣做:

select memberid, this, that, theother 
from 
(
select memberid, this, that, theother, 
     row_number() over (partition by memberid order by this) rn 
from annualfees 
) 
where rn = 1; 

如果你想每MEMBERID特定行,例如在一個與最近的起始日期,那麼你可以將它修改爲:

select memberid, this, that, theother 
from 
(
select memberid, this, that, theother, 
     row_number() over (partition by memberid order by StartDate desc) rn 
from annualfees 
) 
where rn = 1; 
+0

RE:您最後的查詢。你碰巧知道在Oracle中是否比我的答案中的版本更高效? – 2010-06-25 09:48:28

+1

@Martin Smith:分析解決方案(Tony's)將對數據進行一次傳遞,而您的數據將添加一個自聯接(這可能是一個小的開銷,但會增加工作量)。 **總的來說**,Tony的解決方案因此會更有效率。 – 2010-06-25 09:58:55

+0

@Vincent謝謝,我想知道row_number的計算。我認爲在單次傳遞時,它必須爲每個成員id創建一個桶,用開始日期和行ID填充它,然後對每個桶進行排序。如果是這樣,我可以看到這會更快。如果猜測它是如何工作的,那麼有人告訴我這是完全錯誤的! – 2010-06-25 10:28:13

-3

select DISTINCT MemberID,StartingDate,ExpiryDate,Amount from AnnualFees;

刪除paranthesis

+2

這將返回一行*每個排列*的MemberID,StartingDate,ExpiryDate和金額 – APC 2010-06-25 13:03:39

3

您需要選擇與重複MemberIDs行以某種方式返回。這將得到最大的startDate。

SELECT MemberID,StartingDate,ExpiryDate,Amount 
FROM AnnualFees af 
WHERE NOT EXISTS (
     SELECT * from AnnualFees af2 
     WHERE af2.MemberID = af.MemberID 
     AND af2.StartingDate > af.StartingDate) 
5

不知道這是你需要相當的東西,但你可能需要看看GROUP BY而不是DISTINCT ...

如果你有幾條記錄同一部件ID,您可能需要指定exaclty如何識別你從別人想要的

如讓每個member`s最後開始日期:

SELECT memberid, max(startingdate) 
FROM annualfees 
GROUP BY memberid 

,但如果你需要確定一個記錄在這種方式,但也顯示了其他列,我認爲you may need to do some trickery like this ...以上

例如,子查詢選擇與一個連接加入其他你想列:

SELECT subq.memid, subq.startdate, a.expirydate, a.amount 
FROM (
    SELECT memberid AS memid, max(startingdate) AS startdate 
    FROM annualfees 
    GROUP BY memberid) subq 
INNER JOIN annualfees a ON a.memberid = subq.memid 
       AND a.startingdate = subq.startdate 

從開始到結束,也顯示出數據表(O/p被曝/使用 「組驗證ON」 抓住)...

-- show all rows 
select * 
from annualfees 
order by memberid, startingdate 
MEMBERID    STARTINGDATE    EXPIRYDATE   AMOUNT    
---------------------- ------------------------- -------------------- -------------------- 
1      02-DEC-09     05-FEB-10   111     
1      25-JUN-10     25-JUN-11   222     
2      25-APR-10     25-JUN-13   333     

3 rows selected 

/
-- show one member`s data using max(startingdate) as selector. 
SELECT memberid, max(startingdate) 
    FROM annualfees 
    GROUP BY memberid 
MEMBERID    MAX(STARTINGDATE)   
---------------------- ------------------------- 
1      25-JUN-10     
2      25-APR-10     

2 rows selected 

/
-- show above data joined with the other columns. 
SELECT subq.memid, subq.startdate, a.expirydate, a.amount 
    FROM (
     SELECT memberid AS memid, max(startingdate) AS startdate 
     FROM annualfees 
     GROUP BY memberid) subq 
    INNER JOIN annualfees a ON a.memberid = subq.memid AND a.startingdate = subq.startdate 
MEMID     STARTDATE     EXPIRYDATE   AMOUNT    
---------------------- ------------------------- -------------------- -------------------- 
1      25-JUN-10     25-JUN-11   222     
2      25-APR-10     25-JUN-13   333     

2 rows selected 

/