Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 6th, 2009, 09:30 AM
Authorized User
 
Join Date: Jan 2009
Posts: 43
Thanks: 0
Thanked 2 Times in 2 Posts
Default from dinesh

ok i will try to do it tmrro. gud luck frnd.
__________________
dinesh s
 
Old February 6th, 2009, 11:28 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default Because, it doesn't work that way...

Quote:
Originally Posted by dinesh View Post
you mean "any two" rite, why dont it be the first two?
Because without an ORDER BY, there is no guarantee as to what the order of rows will be in SQL Server. Even a clustered index doesn't guarantee the order of the return on SELECTs.
__________________
--Jeff Moden
 
Old February 6th, 2009, 04:24 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

For what it is worth, you need to use ORDER BY in *both* the SELECTs that Jeff shows, if you want to control the ordering of the records:
Code:
SELECT *
FROM Departments d1
WHERE EmpName IN ( SELECT TOP 2 EmpName 
                   FROM Departments d2 
                   WHERE d2.DepartmentID = d1.DepartmentID
                   ORDER BY d2.NumberOfKumquatsEaten )
ORDER BY d1.departmentID, NumberOfKumquatsEaten
The field "NumberOfKumquatsEaten" is of course nonsense. Choose a real field that makes sense in your DB/Table. The point is, though, that you *MUST* control the ordering YOURSELF. SQL won't do it for you.
 
Old February 6th, 2009, 07:59 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default But no sort order was stipulated...

The problem with the original problem posted is that OP didn't stipulate anything to sort on.
__________________
--Jeff Moden
 
Old February 6th, 2009, 08:27 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Sure. So don't you think NumberOfKumquatsEaten is as good a guess as any? <grin style="foolish" />
 
Old February 6th, 2009, 08:33 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default BWAA-HAA!

Heh... I have to admit, it's close.
__________________
--Jeff Moden









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.