November 29, 2009 at 6:32 pm
hi All,
We have two table company and people and people has coid as a foreign key. I need to select 10 contacts or less for each company randomly.
Below are the structures:
Company - CoID, Company, address, street, city, state, postcode, country, phone, fax, addedby, addedon, editedby, editedon
People- PersonID, CoID, Firstname, lastname, title, persontype, email, addedon, addedby, editedon, editedby
Thanks for your help, please get me started
November 29, 2009 at 8:49 pm
This should get you started...
WITH
cteRandomlyNumber AS
(
SELECT PersonID,
CoID,
RowNum = ROW_NUMBER() OVER (PARTITION BY CoID ORDER BY NEWID())
FROM dbo.People
)
SELECT PersonID, CoID
FROM cteRandomlyNumber
WHERE RowNum <= 10
Takes about 12 seconds on a million employees across 676 different companies.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 29, 2009 at 9:13 pm
hi,
You are a star it worked like a charm, thanks for your time.
-Shilpa.
November 29, 2009 at 9:21 pm
Thanks for the feedback, Shilpa.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 30, 2009 at 9:43 am
I thought that GUIDs didn't have an order. I am never able to ORDER by a GUID, so why are you able to ORDER BY NEWID() here?
November 30, 2009 at 10:43 am
To produce a random order. It's an old trick.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply