0

我一直在努力让它发挥作用,但我无处可去。我需要的是以下内容:

我需要能够为特定表选择除 MAX 记录之外的所有记录。我知道如何选择 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 记录合并到单独的行中。

谢谢你的帮助。

4

4 回答 4

1

在您的查询中:在第 9 行 - 将其更改为:

SELECT MAX(pn.hmy)

并删除第 20 行。它不需要。

于 2013-03-06T17:21:08.050 回答
0

基本上,你想要这个:

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

于 2013-03-06T17:10:39.857 回答
0
select
    MaxValue = Max(/* whatever*/),
   -- other required columns
from 
:
:
group by -- as needed
having value <> MaxValue
于 2013-03-06T17:10:42.043 回答
0

感谢您的输入..

我更接近了。我的下一个障碍是尝试分离剩余的记录。例如,有多个名称链接到记录。

我希望它是:

新房间,新房间 2

鲍勃·史密斯,乔治·威尔逊

我得到:

新房间,新房间 2

鲍勃史密斯

乔治·威尔逊

我的代码如下:

   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 记录:

MAX_Room, NewRoom, NewRoom2

感谢大家

于 2013-03-07T19:33:53.560 回答