Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
What is a better way to do this?select name, numberfrom A1where ACCTNUM=80989and NB='1'and TF='O'and NC=( select max(NC) FROM A1 where ACCTNUM=80989 and NB='1' )
Kristen
Test
22859 Posts
Posted - 2006-12-08 : 01:10:02
Looks OK to me. I would do a JOIN but that's probably personal preference.Check the query plan and see if one is better than the other.And index on ACCTNUM + NB would help.
select name, numberfrom A1 JOIN ( select [MaxNC] = max(NC) FROM A1 where ACCTNUM=80989 and NB='1' ) AS X ON X.MaxNC = A1.NCwhere ACCTNUM=80989 and NB='1' and TF='O'
SELECT t.Name, t.NumberFROM A1 tINNER JOIN ( SELECT AcctNum, MAX(NC) mnc FROM A1 WHERE NB = '1' GROUP BY AcctNum ) d ON d.AcctNum = t.AcctNum AND d.mnc = t.NCWHERE t.NB = '1' and t.TF = 'O' and t.ACCTNUM = 80989 -- remove this line for all accts
Peter LarssonHelsingborg, Sweden
Kristen
Test
22859 Posts
Posted - 2006-12-08 : 08:36:17
I reckon the inner nested query is too large, and needs to be constrained according to what the outer query is - personally!Kristen