Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create useful-snippets.md with some useful DB queries #74

Merged
merged 1 commit into from
Oct 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/useful-snippets.md
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';
```