newid

Using NEWID() with CTE to produce random subset of rows produces odd results

对着背影说爱祢 提交于 2019-12-01 11:12:46
I'm writing some SQL in a stored procedure to reduce a dataset to a limited random number of rows that I want to report on. The report starts with a Group of Users and a filter is applied to specify the total number of random rows required ( @SampleLimit ). To achieve the desired result, I start by creating a CTE (temp table) with: The top(@SampleLimit) applied group by UserId (as the UserID appears multiple times) order by NEWID() to put the results in a random order SQL: ; with cte_temp as (select top(@SampleLimit) UserId from QueryResults where (GroupId = @GroupId) group by UserId order by

SQL Server : does NEWID() always gives a unique ID?

人盡茶涼 提交于 2019-11-30 17:14:01
Does the NEWID() function never give me the same ID as it already did? Let's say I select a NEWID() and it returns '1' (just as an example). Will it never return '1' again? Is it impossible? JotaBe Both NEWID() and NEWSEQUENTIALID() give globally unique values of type uniqueidentifier . NEWID() involves random activity, thus the next value is unpredictable, and it's slower to execute. NEWSEQUENTIALID() doesn't involve random activity, thus the next generated value can be predicted (not easily!) and executes faster than NEWID() . So, if you're not concerned about the next value being predicted

“order by newid()” - how does it work?

这一生的挚爱 提交于 2019-11-26 09:58:19
问题 I know that If I run this query select top 100 * from mytable order by newid() it will get 100 random records from my table. However, I\'m a bit confused as to how it works, since I don\'t see newid() in the select list. Can someone explain? Is there something special about newid() here? 回答1: I know what NewID() does, I'm just trying to understand how it would help in the random selection. Is it that (1) the select statement will select EVERYTHING from mytable, (2) for each row selected, tack

newid() inside sql server function

喜你入骨 提交于 2019-11-26 08:09:26
问题 I have to insert a fake column at the result of a query, which is the return value of a table-value function. This column data type must be unique-identifier. The best way (I think...) is to use newid() function. The problem is, I can\'t use newid() inside this type of function: Invalid use of side-effecting or time-dependent operator in \'newid()\' within a function. 回答1: here's a clever solution: create view getNewID as select newid() as new_id create function myfunction () returns