Skip to content

Instantly share code, notes, and snippets.

View dorkness-io's full-sized avatar

Dorkness dorkness-io

View GitHub Profile
@dorkness-io
dorkness-io / worklogs.sql
Created March 19, 2020 13:56
Query that sums up worklog entries for a project for the past 7 days. It then ranks and outputs the top 3 projects.
with cte as
(
select
ss.pname
,ss.time_worked
,rank() over (order by ss.time_worked desc) project_rank
from
(
select
p.pname
@dorkness-io
dorkness-io / stored_procedure.sql
Created March 19, 2020 16:07
SQL to create a logging schema and table, create a stored procedure for logging CUD events and implement that stored procedure on a table.
-- Create a special schema to store our logging data
create schema logging;
-- Create a new table within the schema to log our events
create table logging.events
(
id serial
,timestamp timestamptz default current_timestamp
,schema_name text
,table_name text
@dorkness-io
dorkness-io / union.sql
Last active March 19, 2020 18:40
An example to illustrate how unions work. This query unions the os_currentstep and os_historystep table to get the timestamps of all status transitions for issues. You'd be much better off using changegroup/changeitem without a union, but I needed an example :)
select
p.pkey || '-' || ji.issuenum AS issue_key
,cs.start_date
from jiraissue ji
join project p
on ji.project = p.id
join os_currentstep cs
on ji.workflow_id = cs.entry_id
union
@dorkness-io
dorkness-io / aggregates.sql
Created March 19, 2020 19:19
A brief example that retrieves a count of comments per project. We retrieve the counts for each each issue in a sub-select, and then aggregate by project and sum at the top level. Demonstrates the "count" and "sum" aggregate functions.
select
ss.pname
,sum(ss.comment_count) as sum_comments
from (
select
p.pname
,p.pkey || '-' || ji.issuenum AS issue_key
,count(distinct ja.id) as comment_count
from jiraissue ji
join project p
select
p.pkey || '-' || ji.issuenum AS issue_key
from jiraissue ji
join project p
on ji.project = p.id
join jiraaction ja
on ji.id = ja.issueid
and actiontype = 'comment'
group by 1
having count(ja.id) >= 10;
drop table if exists weekly_issues;
select
p.pkey || '-' || ji.issuenum AS issue_key
,ji.summary
,ji.reporter
,ji.created
into temp weekly_issues
from jiraissue ji
join project p
select
p.pkey || '-' || ji.issuenum AS issue_key
,ja.actionbody
from jiraissue ji
inner join project p
on ji.project = p.id
left join jiraaction ja
on ji.id = ja.issueid
and actiontype = 'comment'
where p.pkey = 'TP';
select
p.pkey || '-' || ji.issuenum AS issue_key
,ja.actionbody
from jiraissue ji
inner join project p
on ji.project = p.id
inner join jiraaction ja
on ji.id = ja.issueid
and actiontype = 'comment'
where p.pkey = 'TP';
select
p.pkey || '-' || ji.issuenum AS issue_key
,ja.actionbody
from jiraaction ja
right join jiraissue ji
on ja.issueid = ji.id
join project p
on p.id = ji.project
where p.pkey = 'TP';
select
p.pkey || '-' || ji.issuenum AS issue_key
,ja.actionbody
from jiraaction ja
full outer join jiraissue ji
on ja.issueid = ji.id
join project p
on p.id = ji.project
where p.pkey = 'TP';