-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from thread/docs-useful-snippets
Create useful-snippets.md with some useful DB queries
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Useful snippets | ||
|
||
## Database queries | ||
|
||
### Query current states for all labels | ||
``` | ||
SELECT history.label_name, history.created, history.new_state | ||
FROM history | ||
JOIN ( | ||
SELECT label_name, MAX(created) AS max_created | ||
FROM history | ||
GROUP BY label_name | ||
) latest_states | ||
ON | ||
history.created = latest_states.max_created AND | ||
history.label_name = latest_states.label_name; | ||
``` | ||
|
||
### Count number of labels in a specific state | ||
|
||
``` | ||
SELECT COUNT(*) | ||
FROM history | ||
JOIN ( | ||
SELECT label_name, MAX(created) AS max_created | ||
FROM history | ||
GROUP BY label_name | ||
) latest_states | ||
ON | ||
history.created = latest_states.max_created AND | ||
history.label_name = latest_states.label_name | ||
WHERE history.new_state = 'STATE_NAME'; | ||
``` |