简体   繁体   English

选择除SQL查询中的最大记录以外的所有记录

[英]select all but max record in SQL query

I have been trying to get this to work but I am not getting anywhere. 我一直在努力使它起作用,但是我什么也没得到。 What I need is the following: 我需要的是以下内容:

I need to be able to select all but the MAX record for a particular table. 我需要能够为特定表选择除MAX记录以外的所有记录。 I know how to select the Max record (by using TOP, or MAX) but I want to display all but that. 我知道如何选择最大记录(通过使用TOP或MAX),但我想显示所有内容。 Is there a way to do this? 有没有办法做到这一点? I have tried the code below, but I keep getting the MAX record instead. 我已经尝试了下面的代码,但是我一直在获取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'

After I pull the records, I want to incorporate the MAX record in a separate line. 拉出记录后,我想将MAX记录合并到单独的行中。

Thanks for any help. 谢谢你的帮助。

In your query: On Line 9 - change it to: 在查询中:在第9行-将其更改为:

SELECT MAX(pn.hmy)

And remove Line 20. Its not needed. 并删除第20行。不需要。

基本上,您需要这样:

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

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

Thanks for the input.. 感谢您的输入。

I am a lot closer. 我离我很近。 My next hurdle is trying to separate the remaining records. 我的下一个障碍是尝试分离其余记录。 For instance, there are multiple names linked to the record. 例如,有多个名称链接到记录。

I would like it to be: 我希望它是:

NewRoom, NewRoom2 NewRoom,NewRoom2

Bob Smith, George Wilson 鲍勃·史密斯,乔治·威尔逊

I get: 我得到:

NewRoom, NewRoom2 NewRoom,NewRoom2

BobSmith 鲍勃·史密斯

George Wilson 乔治·威尔逊

My code below: 我的代码如下:

   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'

) and t.scode = 't0029839' )和t.scode ='t0029839'

Not to mention, I need the MAX record on the same line as well: 更不用说,我也需要在同一行上有MAX记录:

MAX_Room, NewRoom, NewRoom2 MAX_Room,NewRoom,NewRoom2

Thanks everyone 感谢大家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM