我一直试图让这个工作,但我没有得到任何地方。我需要的是以下内容:在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记录合并到一个单独的行中。
感谢您的任何帮助。
看看http://stackoverflow.com/questions/6751249/correlated-query-select-where-condition-not-maxcondition-in-inner-query – RandomUs1r 2013-03-06 17:09:25
要删除一行,你可以使用'MINUS'或'SUBTRACT',具体取决于sql的味道。 – Glenn 2013-03-06 17:10:03