Skip to content

Instantly share code, notes, and snippets.

@yoomlam
Last active October 3, 2020 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoomlam/bf720a75fad832d934323a6e09654eb1 to your computer and use it in GitHub Desktop.
Save yoomlam/bf720a75fad832d934323a6e09654eb1 to your computer and use it in GitHub Desktop.
Dispatched AMA Appeals
subq1=select([
func.date_trunc('month', tasksT.columns['closed_at']).label('closed_month'),
tasksT.c.appeal_type,
tasksT.c.appeal_id
])\
.where(and_(
tasksT.c.type.in_(['BvaDispatchTask']),
tasksT.c.status == 'completed',
tasksT.c.closed_at > '2018-10-01 04:00:00'
))\
.group_by(tasksT.c.appeal_id, tasksT.c.appeal_type, 'closed_month')\
.order_by(tasksT.c.appeal_id)
dispatchesQ=subq1.alias('dispatchesByMonth')
stmt=select([func.to_char(dispatchesQ.c.closed_month, 'YYYY-mm').label('monthStr'),
dispatchesQ.c.appeal_type,
func.count().label('count')])\
.group_by(text("1"), text("2"))\
.order_by(text("1"), text("2"))
span=Time.new(2018,10)..Time.new(2019,10)-1
BvaDispatchTask.where(status: :completed, closed_at: span, appeal_type: :Appeal).group(:appeal_id).count
SELECT month, appeal_type, count(*)
FROM (
SELECT
DATE_TRUNC('month', closed_at) AS month
, appeal_type ,appeal_id
FROM tasks
WHERE type IN ('BvaDispatchTask')
AND status = 'completed'
AND closed_at > '2018-10-01 04:00:00'
GROUP BY appeal_id, appeal_type, month
) as subq
GROUP BY 1, 2
ORDER BY 1, 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment