Skip to content

Commit 02a42f8

Browse files
committed
Vale grammar/syntax updates, adding links, formatting, etc.
1 parent 8f3d5b2 commit 02a42f8

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

src/connections/storage/warehouses/redshift-useful-sql.md

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Useful SQL Queries for Redshift
44
Below you'll find a library of some of the most useful SQL queries customers use in their Redshift warehouses. You can run these right in your Redshift instance with little to no modification.
55

66
> note " "
7-
> If you're looking for SQL queries for warehouses other than Redshift, check out some of our [Analyizing with SQL guides](/docs/connections/storage/warehouses/index/#analyzing-with-sql).
7+
> If you're looking for SQL queries for warehouses other than Redshift, check out some of Segment's [Analyzing with SQL guides](/docs/connections/storage/warehouses/index/#analyzing-with-sql).
88
99
## Tracking events
1010

@@ -21,7 +21,7 @@ analytics.track('Completed Order',
2121
});
2222
```
2323

24-
And another completed order track call might look like this:
24+
A completed order track call might look like this:
2525

2626
```javascript
2727
analytics.track('Completed Order', {
@@ -40,7 +40,7 @@ from initech.tracks
4040
where event = 'completed_order'
4141
```
4242

43-
This SQL query returns a table that looks like this:
43+
That SQL query returns a table that looks like this:
4444

4545
| event | event_id | user_id | sent_at | item | color | size | payment |
4646
| --------------- | -------- | ---------- | ------------------- | ----- | ------ | ------ | ----------- |
@@ -50,10 +50,10 @@ This SQL query returns a table that looks like this:
5050
| completed_order | rft31ial | ly3jaeillp | 2021-12-09 08:50:13 | shirt | yellow | Large | credit card |
5151
| completed_order | k8bhgc6h | X9G5Qg0tha | 2021-12-09 07:20:19 | shirt | yellow | Small | paypal |
5252

53-
But why are there columns in the table that weren't a part of our **track** call, like `event_id`?
54-
This is because the track method automatically includes additional properties of the event, like `event_id`, `sent_at`, and `user_id` (for client-side libraries)!
53+
But why are there columns in the table that weren't a part of the track call, like `event_id`?
54+
This is because the track method (for client-side libraries) automatically includes additional properties of the event, like `event_id`, `sent_at`, and `user_id`!
5555

56-
## Grouping events by day
56+
### Grouping events by day
5757
If you want to know how many orders were completed over a span of time, you can use the `date()` and `count` function with the `sent_at` timestamp:
5858

5959
```sql
@@ -93,14 +93,16 @@ That query returns a table like this:
9393
## Defining sessions
9494
Segment’s API does not impose any restrictions on your data with regard to user sessions.
9595

96-
Sessions aren’t fundamental facts about the user experience. They’re stories we build around the data to understand how customers actually use the product in their day-to-day lives. And since Segment’s API is about collecting raw, factual data, we don’t have an API for collecting sessions. We leave session interpretation to our partners, which let you design how you measure sessions based on how customers use your product.
96+
Sessions aren’t fundamental facts about the user experience. They’re stories Segment builds around the data to understand how customers actually use the product in their day-to-day lives. And since Segment’s API is about collecting raw, factual data, there's no API for collecting sessions. Segment leaves session interpretation to SQL partners, which let you design how you measure sessions based on how customers use your product.
97+
98+
For more on why Segment doesn't collect session data at the API level, [check out a blog post here](https://segment.com/blog/facts-vs-stories-why-segment-has-no-sessions-api/)!
9799

98100
### How to define user sessions using SQL
99-
Each of our SQL partners allows you to define sessions based on your specific business needs. With Looker, for example, you can take advantage of their persistent derived tables and LookML modeling language to layer sessionization on top of your Segment SQL data. We recommend checking out their approach here!
101+
Each of Segment's SQL partners allows you to define sessions based on your specific business needs. With [Looker](https://looker.com), for example, you can take advantage of their persistent derived tables and LookML modeling language to layer sessionization on top of your Segment SQL data. Segment recommends [checking out Looker's approach here](https://segment.com/blog/using-sql-to-define-measure-and-analyze-user-sessions/)!
100102

101-
For defining sessions with raw SQL, the best query and explanation we’ve come across is from our friends at Mode Analytics.
103+
For defining sessions with raw SQL, a great query and explanation comes from [Mode Analytics](https://mode.com).
102104

103-
Here’s the query to make it happen, but we definitely recommend checking out their blog post as well! They walk you through the reasoning behind the query, what each portion accomplishes, how you can tweak it to suit your needs, and what kind of further analysis you can do on top of it.
105+
Here’s the query to make it happen, but definitely check Mode Analytics' [blog post](https://blog.modeanalytics.com/finding-user-sessions-sql/) as well. They walk you through the reasoning behind the query, what each portion accomplishes, how you can tweak it to suit your needs, and what kind of further analysis you can do on top of it.
104106

105107
```sql
106108
-- Finding the start of every session
@@ -142,7 +144,7 @@ analytics.identify('bob123',{
142144
plan: 'Free'
143145
});
144146
```
145-
As these user traits change over time, you can continue calling the identify method to update their changes. Here we’ll update Bob’s account plan to “Premium”.
147+
As these user traits change over time, you can continue calling the identify method to update their changes. With this query, you can update Bob’s account plan to “Premium”.
146148

147149
```javascript
148150
analytics.identify('bob123', {
@@ -163,14 +165,14 @@ This SQL query returns a table of Bob's account information, with each entry rep
163165

164166
| user_id | email | plan | sent_at |
165167
| ------- | -------------- | ------- | ------------------- |
166-
| bob123 | [email protected] | Premium | 2013-12-20 19:44:03 |
167-
| bob123 | [email protected] | Basic | 2013-12-18 17:48:10 |
168+
| bob123 | [email protected] | Premium | 2021-12-20 19:44:03 |
169+
| bob123 | [email protected] | Basic | 2021-12-18 17:48:10 |
168170

169-
If you want to see what your users looked like at a previous point in time, that data is right there in your `identifies` table! (To get this table for your users, replace ‘initech’ with your source slug).
171+
If you want to see what your users looked like at a previous point in time, that data is right there in your `identifies` table! (To get this table for your users, replace ‘initech’ in the SQL query with your source slug).
170172

171-
But what if you only want to see the most recent state of the user? Luckily, we can convert the `identifies` table into a distinct users table by taking the most recent identify call for each account.
173+
But what if you only want to see the most recent state of the user? Luckily, you can convert the `identifies` table into a distinct users table by taking the most recent identify call for each account.
172174

173-
## Converting The identifies table into a users table
175+
### Converting the identifies table into a users table
174176

175177
The following query will return your `identifies` table:
176178

@@ -182,12 +184,12 @@ That query returns a table like this:
182184

183185
| user_id | email | plan | sent_at |
184186
| ------- | -------------- | ------- | ------------------- |
185-
| bob123 | [email protected] | Premium | 2013-12-20 19:44:03 |
186-
| bob123 | [email protected] | Basic | 2013-12-18 17:48:10 |
187-
| jeff123 | [email protected] | Premium | 2013-12-20 19:44:03 |
188-
| jeff123 | [email protected] | Basic | 2013-12-18 17:48:10 |
187+
| bob123 | [email protected] | Premium | 2021-12-20 19:44:03 |
188+
| bob123 | [email protected] | Basic | 2021-12-18 17:48:10 |
189+
| jeff123 | [email protected] | Premium | 2021-12-20 19:44:03 |
190+
| jeff123 | [email protected] | Basic | 2021-12-18 17:48:10 |
189191

190-
if all you want is a table of distinct user with their current traits and without duplicates, you can do so with the following query:
192+
If all you want is a table of distinct user with their current traits and without duplicates, you can do so with the following query:
191193

192194
```sql
193195
with identifies as (
@@ -210,17 +212,17 @@ select *
210212
from users
211213
```
212214

213-
## Counts of user traits
215+
### Counts of user traits
214216
Let's say you have an `identifies` table that looks like this:
215217

216218
| user_id | email | plan | sent_at |
217219
| ------- | -------------- | ------- | ------------------- |
218-
| bob123 | [email protected] | Premium | 2013-12-20 19:44:03 |
219-
| bob123 | [email protected] | Basic | 2013-12-18 17:48:10 |
220-
| jeff123 | [email protected] | Premium | 2013-12-20 19:44:03 |
221-
| jeff123 | [email protected] | Basic | 2013-12-18 17:48:10 |
220+
| bob123 | [email protected] | Premium | 2021-12-20 19:44:03 |
221+
| bob123 | [email protected] | Basic | 2021-12-18 17:48:10 |
222+
| jeff123 | [email protected] | Premium | 2021-12-20 19:44:03 |
223+
| jeff123 | [email protected] | Basic | 2021-12-18 17:48:10 |
222224

223-
If we want to query the traits of these users, we first need to [convert the identifies table into a users table](#converting-the-identifies-table-into-a-users-table) From there, we can run a query like this to get a count of users with each type of plan:
225+
If you want to query the traits of these users, you first need to [convert the identifies table into a users table](#converting-the-identifies-table-into-a-users-table). From there, run a query like this to get a count of users with each type of plan:
224226

225227
```sql
226228
with identifies as (
@@ -252,7 +254,7 @@ And there you go: a count of users with each type of plan!
252254

253255
### Historical Traits
254256

255-
The `group` method ties a user to a group. Be it a company, organization, account, source, team or whatever other crazy name you came up with for the same concept! It also lets you record custom traits about the group, like industry or number of employees.
257+
The `group` method ties a user to a group. It also lets you record custom traits about the group, like the industry or number of employees.
256258

257259
Here’s what a basic `group` call looks like:
258260

@@ -288,11 +290,11 @@ The previous query will return a table of Initech's group information, with each
288290
| name | industry | employees | plan | sent_at |
289291
| ------- | ---------- | --------- | ------- | ------------------- |
290292
| Initech | Technology | 600 | Premium | 2021-12-20 19:44:03 |
291-
| Initech | Technology | 349 | Free | 2013-12-18 17:18:15 |
293+
| Initech | Technology | 349 | Free | 2021-12-18 17:18:15 |
292294

293-
If you want to see a group’s traits at a previous point in time, that data is right there in your groups table! (To get this table for your groups, replace ‘initech’ with your source slug).
295+
If you want to see a group’s traits at a previous point in time, this query is useful (To get this table for your groups, replace ‘initech’ with your source slug).
294296

295-
But what if you only want to see the most recent state of the group? You can convert the groups table into a distinct groups table by taking the most recent groups call for each account.
297+
If you only want to see the most recent state of the group, you can convert the groups table into a distinct groups table by viewing the most recent groups call for each account.
296298

297299
### Converting the Groups Table into an Organizations Table
298300

@@ -308,9 +310,9 @@ The previous query returns the following table:
308310
| name | industry | employees | plan | sent_at |
309311
| --------- | ------------- | --------- | ------- | ------------------- |
310312
| Initech | Technology | 600 | Premium | 2021-12-20 19:44:03 |
311-
| Initech | Technology | 349 | Free | 2013-12-18 17:18:15 |
313+
| Initech | Technology | 349 | Free | 2021-12-18 17:18:15 |
312314
| Acme Corp | Entertainment | 15 | Premium | 2021-12-20 19:44:03 |
313-
| Acme Corp | Entertainment | 10 | Free | 2013-12-18 17:18:15 |
315+
| Acme Corp | Entertainment | 10 | Free | 2021-12-18 17:18:15 |
314316

315317
However, if all you want is a table of distinct groups and current traits, you can do so with the following query:
316318

@@ -336,7 +338,7 @@ organizations as (
336338
select *
337339
from organizations
338340
```
339-
This query will retun a table with your distinct groups, without duplicates!
341+
This query will return a table with your distinct groups, without duplicates.
340342

341343
| name | industry | employees | plan | sent_at |
342344
| --------- | ------------- | --------- | ------- | ------------------- |

0 commit comments

Comments
 (0)