{
  "db_name": "PostgreSQL",
  "query": "\n            WITH marked AS (\n                UPDATE credits_transactions\n                SET is_aggregated = true\n                WHERE user_id = $1\n                  AND fusillade_batch_id IS NOT NULL\n                  AND is_aggregated = false\n                RETURNING fusillade_batch_id, amount, seq, created_at\n            ),\n            aggregated AS (\n                SELECT\n                    fusillade_batch_id,\n                    SUM(amount) as total_amount,\n                    COUNT(*) as tx_count,\n                    MAX(seq) as max_seq,\n                    MIN(created_at) as created_at\n                FROM marked\n                GROUP BY fusillade_batch_id\n            )\n            INSERT INTO batch_aggregates (fusillade_batch_id, user_id, total_amount, transaction_count, max_seq, created_at, updated_at)\n            SELECT fusillade_batch_id, $1, total_amount, tx_count::int, max_seq, created_at, NOW()\n            FROM aggregated\n            ON CONFLICT (fusillade_batch_id) DO UPDATE SET\n                total_amount = batch_aggregates.total_amount + EXCLUDED.total_amount,\n                transaction_count = batch_aggregates.transaction_count + EXCLUDED.transaction_count,\n                max_seq = GREATEST(batch_aggregates.max_seq, EXCLUDED.max_seq),\n                updated_at = NOW()\n            RETURNING fusillade_batch_id\n            ",
  "describe": {
    "columns": [
      {
        "ordinal": 0,
        "name": "fusillade_batch_id",
        "type_info": "Uuid"
      }
    ],
    "parameters": {
      "Left": [
        "Uuid"
      ]
    },
    "nullable": [
      false
    ]
  },
  "hash": "9a4e19b04ec7fc0e456e135bbe1497e621c2502076c4d0183123d581808bab70"
}
