Skip to content

Commit

Permalink
Update useful-snippets.md
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron7 committed Dec 6, 2018
1 parent 19afdf5 commit 17db5ac
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions docs/useful-snippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,50 @@

## Database queries

### Query current states for all labels
### Number of labels in each state
```
SELECT latest_states.new_state, COUNT(*) AS num_labels_in_state
FROM (
SELECT history.label_name, history.new_state
FROM history
JOIN (
SELECT label_name, MAX(id) AS max_id
FROM history
GROUP BY label_name
) states
ON
history.id = states.max_id AND
history.label_name = states.label_name
) latest_states
GROUP BY latest_states.new_state;
```

### Latest state for all labels
```
SELECT history.label_name, history.created, history.new_state
FROM history
JOIN (
SELECT label_name, MAX(created) AS max_created
SELECT label_name, MAX(id) AS max_id
FROM history
GROUP BY label_name
) latest_states
ON
history.created = latest_states.max_created AND
history.id = latest_states.max_id AND
history.label_name = latest_states.label_name;
```

### Count number of labels in a specific state
### Number of labels in a specific state

```
SELECT COUNT(*)
FROM history
JOIN (
SELECT label_name, MAX(created) AS max_created
SELECT label_name, MAX(id) AS max_id
FROM history
GROUP BY label_name
) latest_states
ON
history.created = latest_states.max_created AND
history.id = latest_states.max_id AND
history.label_name = latest_states.label_name
WHERE history.new_state = 'STATE_NAME';
```

0 comments on commit 17db5ac

Please sign in to comment.