Reputation: 813
I need some help to understand an SQL-statement:
select customerID as customerid, RAND() as random
from customer tablesample system(10)
fetch first row only with ur
It just doesn't make sense to me. Can anyone tell me what it actually means? Is it even a correct SQL?
Some further questions:
Is "as customerid" necessary?
Is "as random" necessary?
What does "tablesample system(10)" mean?
What does "with ur" mean?
Is it necessary to have Rand() and tablesample system(10) and with ur all together?
Thank your for your help
Upvotes: 0
Views: 216
Reputation: 451
The "as customerid" and "as random" are just labels for the resultset, so they are not necessary (if you don't include them, the labels would be the columns names, i.e customerID and RAND() ) RAND() takes a random number between 0 and 1 and associated it with an ID from the results fetch first row would select the first row for your query
tablesample system(10): it selects just a portion from the table, depending on the number you provide (10 means 10%) : you can read about it here https://wiki.postgresql.org/wiki/TABLESAMPLE_Implementation
with UR means 'Uncommited Read' you can learn about it here http://www.anesi.com/v41004.htm
What the query would do is select the customerID from a random 10% of your table, and associate each one of them to a random number between 0 and 1, and then take the first one of these selected customerID and their respective random numbers and return it in a table with the labels "customerid" and "random".
Hope this answers your question.
Upvotes: 2