2013-03-06 129 views
0

我一直試圖讓這個工作,但我沒有得到任何地方。我需要的是以下內容:在SQL查詢中選擇除了最大記錄以外的所有記錄

我需要能夠爲特定表格選擇除MAX記錄以外的所有記錄。我知道如何選擇最大記錄(通過使用TOP或MAX),但我想顯示除此之外的所有內容。有沒有辦法做到這一點?我已經嘗試了下面的代碼,但我一直在獲取MAX記錄。

SELECT 
    rtrim(ltrim(pn.sFirstName + ' ' + pn.uLastName)) as newroom 
    FROM tenant t (nolock) 
    INNER JOIN room rm (NOLOCK) on t.hmyperson = rm.hmytenant 
      and isnull(rm.boccupant,0)=0 
      and rm.dtmoveout is null 
    INNER JOIN person pn (nolock) on pn.hmy = rm.hmyperson 
    WHERE pn.hmy <> 
    (SELECT TOP 1 pn.hmy 
    FROM tenant t (nolock) 
    INNER JOIN property p (nolock) on p.hMy = t.hProperty 
    INNER JOIN unit u (nolock) on (t.hUnit = u.hMy 
    INNER JOIN addr ua (nolock) on u.hmy = ua.hPointer 
    INNER JOIN room rm (NOLOCK) on t.hmyperson = rm.hmytenant 
      and isnull(rm.boccupant,0)=0 
     and rm.dtmoveout is null 
     and isnull(rm.dtMoveIn,getdate()) >= getdate() 
    INNER JOIN person pn (nolock) on pn.hmy = rm.hmyperson 
    WHERE t.code = '011212' 
    ORDER BY pn.hmy) 
    and t.code = '011212' 

當我拉出記錄後,我想將MAX記錄合併到一個單獨的行中。

感謝您的任何幫助。

+1

看看http://stackoverflow.com/questions/6751249/correlated-query-select-where-condition-not-maxcondition-in-inner-query – RandomUs1r 2013-03-06 17:09:25

+0

要刪除一行,你可以使用'MINUS'或'SUBTRACT',具體取決於sql的味道。 – Glenn 2013-03-06 17:10:03

回答

1

在您的查詢:第9行 - 將其更改爲:

SELECT MAX(pn.hmy) 

和刪除線20.沒有必要。

+0

謝謝。我將它與Praveen的解決方案配合使用。還有一個問題 - 是否有辦法將MAX記錄合併到同一個SQL語句中?我的結果如下所示:最大記錄,所有其他記錄(在一行中)。最多可以有4個符合「所有其他記錄」的名稱。謝謝 – 2013-03-07 16:32:39

+0

將其標記爲有用,以便它可以幫助其他人:) – 2013-03-07 16:37:20

0

基本上,你想這樣:

SELECT * FROM tableA WHERE tableA.ID < (SELECT MAX(ID) FROM tableA)

0
select 
    MaxValue = Max(/* whatever*/), 
    -- other required columns 
from 
: 
: 
group by -- as needed 
having value <> MaxValue 
0

感謝輸入..

我拉近了許多。我的下一個障礙是試圖分離剩餘的記錄。例如,有多個名稱鏈接到該記錄。

我想它是:

NewRoom,NewRoom2

鮑勃·史密斯, 喬治·威爾遜

我得到:

NewRoom,NewRoom2

BobSmith

George Wils在

我下面的代碼:

SELECT 
rtrim(ltrim(pn.sFirstName + ' ' + pn.uLastName)) as newroom, 
rtrim(ltrim(pn1.sFirstName + ' ' + pn1.uLastName))as newroom2 
FROM tenant t (nolock) 
INNER JOIN room rm (NOLOCK) on t.hmyperson = rm.hmytenant 
     and isnull(rm.boccupant,0)=0 
     and rm.dtmoveout is null 
INNER JOIN person pn (nolock) on pn.hmy = rm.hmyperson 
LEFT OUTER JOIN room rm1 (NOLOCK) on t.hmyperson = rm1.hmytenant 
     and isnull(rm1.boccupant,0)=0 
     and rm1.dtmoveout is null 
LEFT OUTER JOIN person pn1 (nolock) on pn1.hmy = rm1.hmyperson 
WHERE (pn.hmy or pn1.hmy) <> 
    (SELECT Max(pn.hmy) 
FROM tenant t (nolock) 
INNER JOIN property p (nolock) on p.hMy = t.hProperty 
INNER JOIN unit u (nolock) on (t.hUnit = u.hMy 
        and u.sCode not in ('WAIT' ,'COMAREA') 
        and u.scode not like 'NONRES%' 
        and u.exclude = '0') 
INNER JOIN addr ua (nolock) on u.hmy = ua.hPointer 
INNER JOIN room rm (NOLOCK) on t.hmyperson = rm.hmytenant 
     and isnull(rm.boccupant,0)=0 
    and rm.dtmoveout is null 
INNER JOIN person pn (nolock) on pn.hmy = rm.hmyperson 
WHERE t.scode = 't0029839' 

) 和t.scode = 't0029839'

更何況,我需要在同一行的最大記錄,以及:

MAX_Room,新房,新房2

謝謝大家