# Query

Simple SQL query builder. Helps to do 1 or multiple of these steps:

1. adding computed columns
2. filtering your data
3. summarize columns across rows
4. sorting your data

The order of operations in the SQL follows the list above.

New\_Columns: Takes a SQL expression and adds it to the table as a new column. i.e.: LEFT(ACCOUNT\_ID, 5)

Filters: Adds row filters to the WHERE part of the query

Summarize and Group\_By: Aggregate a column and group by another column

Order\_By\_Columns and Order\_By\_Direction: Order the final table by one or more columns

## Parameters

| Name                 | Type              | Description                                                                                                                                                                                                                                                                                | Is Optional |
| -------------------- | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- |
| new\_columns         | math\_list        | One or more SQL expressions to create new calculated columns in your table                                                                                                                                                                                                                 | True        |
| filters              | filter\_list      | Remove rows using filter logic on one or more columns                                                                                                                                                                                                                                      | True        |
| summarize            | column\_agg\_list | Columns to summarize                                                                                                                                                                                                                                                                       | True        |
| group\_by            | column\_list      | One or more columns to group by A categorical column by which to pivot the calculated metrics. Including this argument will generate a new metric calculation for each distinct value in the group by column. If this column has more than 20 distinct values, the plot will not generate. | True        |
| order\_by\_columns   | column\_list      | Pick one or more columns to order the data by                                                                                                                                                                                                                                              | True        |
| order\_by\_direction | sort\_direction   | Order the data ASC or DESC                                                                                                                                                                                                                                                                 | True        |

## Example

```python
ds = rasgo.get.dataset(id)

ds2 = ds.query(
  new_columns=['LEFT(LEAD_SOURCE, 3)'],
  filters=[LEAD_SOURCE = 'Outbound'],
  summarize={
          'ID': ['COUNT'],
          'AMOUNT':['SUM']},
  group_by=['STAGE_NAME'],
  order_by_columns=['STAGE_NAME'],
  order_by_direction='ASC')

ds2.preview()
```

## Source Code

{% embed url="<https://github.com/rasgointelligence/RasgoTransforms/blob/main/rasgotransforms/rasgotransforms/transforms/query/snowflake/query.sql>" %}
