Hi,
I have a table of products. Each product has a primary key (ID) and a foreign key (SubcategoryID). I want to randomly select 4 rows where SubcategoryID equals a given parameter.
How do I do this?
TIA,
Matt
Printable View
Hi,
I have a table of products. Each product has a primary key (ID) and a foreign key (SubcategoryID). I want to randomly select 4 rows where SubcategoryID equals a given parameter.
How do I do this?
TIA,
Matt
If row repeatation is not an issue than you an use
Code:SELECT TOP 4 * FROM Product WHERE SubcategoryID = Value
Try this:
SQL Code:
SELECT TOP 4 * FROM Product WHERE SubcategoryID = Value ORDER BY NEWID()
So what actually happens is that newid() returns a new GUID/unique identifier for every row in the result set, it then sorts based on the GUID value and then eventually selects the 4 top most rows.
But that just returns the same top 4 every time. is there a way to pick a random 4?
Must have crossed post #3
o I didn't see that. I thought it was part of your sig.
Sweet, I'll give it a try!
TY
gary,
I'm getting an error on Value. It says it's an invalid column.
i'm sorry. brain fart. I got it. Thanks!!
If this resolves this issue could you please mark this thread as RESOLVED using the Thread Tools above. This will let anyone else looking for a similar issue that you have recieved a good answer to this question.