Return a :class:`_expression.TableSample` object. :class:`_expression.TableSample` is an :class:`_expression.Alias` subclass that represents a table with the TABLESAMPLE clause applied to it. :func:`_expression.tablesample` is also available from the :class:`_expression.FromClau
(
selectable: _FromClauseArgument,
sampling: Union[float, Function[Any]],
name: Optional[str] = None,
seed: Optional[roles.ExpressionElementRole[Any]] = None,
)
| 638 | |
| 639 | |
| 640 | def tablesample( |
| 641 | selectable: _FromClauseArgument, |
| 642 | sampling: Union[float, Function[Any]], |
| 643 | name: Optional[str] = None, |
| 644 | seed: Optional[roles.ExpressionElementRole[Any]] = None, |
| 645 | ) -> TableSample: |
| 646 | """Return a :class:`_expression.TableSample` object. |
| 647 | |
| 648 | :class:`_expression.TableSample` is an :class:`_expression.Alias` |
| 649 | subclass that represents |
| 650 | a table with the TABLESAMPLE clause applied to it. |
| 651 | :func:`_expression.tablesample` |
| 652 | is also available from the :class:`_expression.FromClause` |
| 653 | class via the |
| 654 | :meth:`_expression.FromClause.tablesample` method. |
| 655 | |
| 656 | The TABLESAMPLE clause allows selecting a randomly selected approximate |
| 657 | percentage of rows from a table. It supports multiple sampling methods, |
| 658 | most commonly BERNOULLI and SYSTEM. |
| 659 | |
| 660 | e.g.:: |
| 661 | |
| 662 | from sqlalchemy import func |
| 663 | |
| 664 | selectable = people.tablesample( |
| 665 | func.bernoulli(1), name="alias", seed=func.random() |
| 666 | ) |
| 667 | stmt = select(selectable.c.people_id) |
| 668 | |
| 669 | Assuming ``people`` with a column ``people_id``, the above |
| 670 | statement would render as: |
| 671 | |
| 672 | .. sourcecode:: sql |
| 673 | |
| 674 | SELECT alias.people_id FROM |
| 675 | people AS alias TABLESAMPLE bernoulli(:bernoulli_1) |
| 676 | REPEATABLE (random()) |
| 677 | |
| 678 | :param sampling: a ``float`` percentage between 0 and 100 or |
| 679 | :class:`_functions.Function`. |
| 680 | |
| 681 | :param name: optional alias name |
| 682 | |
| 683 | :param seed: any real-valued SQL expression. When specified, the |
| 684 | REPEATABLE sub-clause is also rendered. |
| 685 | |
| 686 | """ |
| 687 | return TableSample._factory(selectable, sampling, name=name, seed=seed) |
| 688 | |
| 689 | |
| 690 | @overload |