Possible Duplicate:
How to randomly select rows in SQL?
Suppose that -- in Teradata 12 -- I have a table TXN that has Transaction_ID and Store_nbr. Suppose also that I have a table SAMP that has Store_nbr and n_samples.
Suppose I want to take a sample of n_samples Transaction_IDs from TXN. I.e., I want the SAMP table to be able to drive how many transaction_id samples are taken from the TXN table. E.g.,
TXN
Transaction_ID Store_nbr
1 1
2 1
1 2
2 2
3 2
...and...
SAMP
store_nbr n_samples
1 2
2 2
3 1
So how can I tell SQL (this is Teradata, btw): Give me 2 samples of transaction_Id for store 1, 2 samples of transaction_Id for store 2, and 1 sample of transaction_Id for store 3?
Teradata does have the conditional sample concept:
select a,b
from foo
sample when a=1 then 5
when a=2 then 10
when a=3 then 7
end
...which I could use a codegen method to create if needed (there are thousands of stores).
The SAMP table is free to change at any time.