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
+14-14Lines changed: 14 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
---
2
2
title: Useful SQL Queries for Redshift
3
3
---
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.
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 in your Redshift instance with little to no modification.
5
5
6
6
> success "Ways to improve query speed"
7
-
> If you're looking to improve the speed of your queries, check out Segment's [Speeding Up Redshift Queries](/docs/connections/storage/warehouses/redshift-tuning/) page!
7
+
> If you're looking to improve the speed of your queries, check out Segment's [Speeding Up Redshift Queries](/docs/connections/storage/warehouses/redshift-tuning/) page.
8
8
9
9
You can use SQL queries for the following tasks:
10
10
-[Tracking events](#tracking-events)
@@ -54,7 +54,7 @@ That SQL query returns a table that looks like this:
54
54

55
55
56
56
But why are there columns in the table that weren't a part of the track call, like `event_id`?
57
-
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`!
57
+
This is because the track method (for client-side libraries) includes additional properties of the event, like `event_id`, `sent_at`, and `user_id`!
58
58
59
59
### Grouping events by day
60
60
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:
@@ -65,15 +65,15 @@ from initech.tracks
65
65
where event ='completed_order'
66
66
group bydate
67
67
```
68
-
That query will return a table like this:
68
+
That query returns a table like this:
69
69
70
70
| date | count |
71
71
| ---------- | ----- |
72
72
| 2021-12-09 | 5 |
73
73
| 2021-12-08 | 3 |
74
74
| 2021-12-07 | 2 |
75
75
76
-
If you wanted to see how many pants and shirts were sold on each of those dates, you can query that using case statements:
76
+
To see the number of pants and shirts that were sold on each of those dates, you can query that using case statements:
77
77
78
78
```sql
79
79
selectdate(sent_at) asdate,
@@ -93,19 +93,19 @@ That query returns a table like this:
93
93
| 2021-12-07 | 2 | 0 |
94
94
95
95
96
-
## Defining sessions
96
+
## Define sessions
97
97
Segment’s API does not impose any restrictions on your data with regard to user sessions.
98
98
99
99
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.
100
100
101
-
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/)!
101
+
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/){:target="_blank"}.
102
102
103
103
### How to define user sessions using SQL
104
-
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/)!
104
+
Each of Segment's SQL partners allow you to define sessions based on your specific business needs. With [Looker](https://looker.com){:target="_blank"}, 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/).
105
105
106
-
For defining sessions with raw SQL, a great query and explanation comes from [Mode Analytics](https://mode.com).
106
+
To define sessions with raw SQL, a great query and explanation comes from [Mode Analytics](https://mode.com).
107
107
108
-
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.
108
+
Here’s the query to make it happen, but read Mode Analytics' [blog post](https://blog.modeanalytics.com/finding-user-sessions-sql/)for more information. Mode walks you through the reasoning behind the query, what each portion accomplishes, how you can tweak it to suit your needs, and the kinds of further analysis you can add on top of it.
109
109
110
110
```sql
111
111
-- Finding the start of every session
@@ -135,7 +135,7 @@ SELECT *,
135
135
) final
136
136
```
137
137
138
-
## Identifying users
138
+
## Identify users
139
139
140
140
### Historical traits
141
141
@@ -171,13 +171,13 @@ 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’ in the SQL query with your source slug).
174
+
If you want to see what your users looked like at a previous point in time, you can find that data in the`identifies` table. To get this table for your users, replace ‘initech’ in the SQL query with your source slug.
175
175
176
176
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.
177
177
178
-
### Converting the identifies table into a users table
178
+
### Convert the identifies table into a users table
179
179
180
-
The following query will return your`identifies` table:
180
+
The following query returns the`identifies` table:
0 commit comments