{
  "db_name": "PostgreSQL",
  "query": "\n            SELECT COALESCE(SUM(signed_amount), 0) as \"sum!\"\n            FROM (\n                SELECT * FROM (\n                    (SELECT\n                        ba.max_seq,\n                        -ba.total_amount as signed_amount\n                    FROM batch_aggregates ba\n                    WHERE ba.user_id = $1\n                      AND ($3::timestamptz IS NULL OR ba.created_at >= $3)\n                      AND ($4::timestamptz IS NULL OR ba.created_at <= $4)\n                    ORDER BY ba.max_seq DESC\n                    LIMIT $2)\n\n                    UNION ALL\n\n                    -- Non-batched transactions\n                    (SELECT\n                        ct.seq as max_seq,\n                        CASE WHEN ct.transaction_type IN ('admin_grant', 'purchase')\n                            THEN ct.amount\n                            ELSE -ct.amount\n                        END as signed_amount\n                    FROM credits_transactions ct\n                    WHERE ct.user_id = $1\n                      AND ct.fusillade_batch_id IS NULL\n                      AND ($3::timestamptz IS NULL OR ct.created_at >= $3)\n                      AND ($4::timestamptz IS NULL OR ct.created_at <= $4)\n                    ORDER BY ct.seq DESC\n                    LIMIT $2)\n                ) combined\n                ORDER BY max_seq DESC\n                LIMIT $2\n            ) recent\n            ",
  "describe": {
    "columns": [
      {
        "ordinal": 0,
        "name": "sum!",
        "type_info": "Numeric"
      }
    ],
    "parameters": {
      "Left": [
        "Uuid",
        "Int8",
        "Timestamptz",
        "Timestamptz"
      ]
    },
    "nullable": [
      null
    ]
  },
  "hash": "86974460e9973cff43f1ddfcfbabea2821f8bb748fe11f64b1e4120d8cf8a100"
}
