You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/connections/storage/warehouses/redshift-useful-sql.md
+36-34Lines changed: 36 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: Useful SQL Queries for Redshift
4
4
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.
5
5
6
6
> 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).
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`!
55
55
56
-
## Grouping events by day
56
+
###Grouping events by day
57
57
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:
58
58
59
59
```sql
@@ -93,14 +93,16 @@ That query returns a table like this:
93
93
## Defining sessions
94
94
Segment’s API does not impose any restrictions on your data with regard to user sessions.
95
95
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/)!
97
99
98
100
### 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/)!
100
102
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).
102
104
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.
104
106
105
107
```sql
106
108
-- Finding the start of every session
@@ -142,7 +144,7 @@ analytics.identify('bob123',{
142
144
plan:'Free'
143
145
});
144
146
```
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”.
146
148
147
149
```javascript
148
150
analytics.identify('bob123', {
@@ -163,14 +165,14 @@ This SQL query returns a table of Bob's account information, with each entry rep
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).
170
172
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.
172
174
173
-
## Converting The identifies table into a users table
175
+
###Converting the identifies table into a users table
174
176
175
177
The following query will return your `identifies` table:
176
178
@@ -182,12 +184,12 @@ That query returns a table like this:
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:
224
226
225
227
```sql
226
228
with identifies as (
@@ -252,7 +254,7 @@ And there you go: a count of users with each type of plan!
252
254
253
255
### Historical Traits
254
256
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.
256
258
257
259
Here’s what a basic `group` call looks like:
258
260
@@ -288,11 +290,11 @@ The previous query will return a table of Initech's group information, with each
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).
294
296
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.
296
298
297
299
### Converting the Groups Table into an Organizations Table
298
300
@@ -308,9 +310,9 @@ The previous query returns the following table:
0 commit comments