SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 get set of random rows with distinct values
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jstranger
Starting Member

United Kingdom
11 Posts

Posted - 06/17/2013 :  07:38:46  Show Profile  Reply with Quote
I have an interesting little T-SQL problem that has me beaten.

I want to retrieve a small set of randomly selected rows from a table but also ensure that a particular column (AUTHORID) is unique (not duplicated) within that set.

SELECT TOP 6 WORKID, AUTHORID FROM dbo.LITERARYWORKS
ORDER BY CHECKSUM(NEWID())

works fine at getting a random selection, but of course does not prevent duplicates on AUTHORID. And I can't use DISTINCT without including WORKID and AUTHORID in the ORDER BY - and in any case the rows would always be unique if WORKID is included. I believe one way of achieving the effect of a 'DISTINCT' on a single column while returning other columns as well is by using GROUP BY, but I can't see how to use that in combination with ORDER BY.

Any ideas?



Jon Stranger

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 06/17/2013 :  08:01:30  Show Profile  Reply with Quote
--Is it ok?
SELECT TOP 6 MAX(WORKID) WORKID, AUTHORID
FROM dbo.LITERARYWORKS
GROUP BY AUTHORID
ORDER BY CHECKSUM(NEWID())

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 06/17/2013 :  09:17:34  Show Profile  Reply with Quote

SELECT TOP 6 WORKID,AUTHORID
FROM
(
SELECT WORKID, AUTHORID,ROW_NUMBER() OVER (PARTITION BY AUTHORID ORDER BY WORKID DESC) AS Seq 
FROM dbo.LITERARYWORKS
)t
WHERE Seq=1
ORDER BY CHECKSUM(NEWID())


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 06/17/2013 :  23:55:38  Show Profile  Reply with Quote
quote:
Originally posted by visakh16


SELECT TOP 6 WORKID,AUTHORID
FROM
(
SELECT WORKID, AUTHORID,ROW_NUMBER() OVER (PARTITION BY AUTHORID ORDER BY WORKID DESC) AS Seq 
FROM dbo.LITERARYWORKS
)t
WHERE Seq=1
ORDER BY CHECKSUM(NEWID())


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs



Hi visakh,
My solution is also same as yours....
which one is optimized code?

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 06/18/2013 :  00:24:01  Show Profile  Reply with Quote
quote:
Originally posted by bandi

quote:
Originally posted by visakh16


SELECT TOP 6 WORKID,AUTHORID
FROM
(
SELECT WORKID, AUTHORID,ROW_NUMBER() OVER (PARTITION BY AUTHORID ORDER BY WORKID DESC) AS Seq 
FROM dbo.LITERARYWORKS
)t
WHERE Seq=1
ORDER BY CHECKSUM(NEWID())


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs



Hi visakh,
My solution is also same as yours....
which one is optimized code?

--
Chandu


Test it out yourself
I dont have a sql box to text this here

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 06/18/2013 :  00:32:40  Show Profile  Reply with Quote
In your query there are additional 3 operations (Filter, Sequence Project and segment)....
My query had "Stream Aggregate" instead of the above 3 operations...

Total cost is same for both queries ( with my sample data).. But it may differ based on original data...

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 06/18/2013 :  00:51:07  Show Profile  Reply with Quote
quote:
Originally posted by bandi

In your query there are additional 3 operations (Filter, Sequence Project and segment)....
My query had "Stream Aggregate" instead of the above 3 operations...

Total cost is same for both queries ( with my sample data).. But it may differ based on original data...

--
Chandu


Ok...thanks for the info

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 06/18/2013 :  00:53:42  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

quote:
Originally posted by bandi

In your query there are additional 3 operations (Filter, Sequence Project and segment)....
My query had "Stream Aggregate" instead of the above 3 operations...

Total cost is same for both queries ( with my sample data).. But it may differ based on original data...
--
Chandu


Ok...thanks for the info




--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000