> For the complete documentation index, see [llms.txt](https://documentarians.gitbook.io/metadocumentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://documentarians.gitbook.io/metadocumentation/master/internal/internal/queries/gift-cards.md).

# Gift Cards

## Gift cards created and claims per month

```
SELECT 
date_trunc('month', pm."createdAt") as "month",
count(*),
sum(
CASE
WHEN pm."CollectiveId" = spm."CollectiveId" THEN 0
WHEN pm."CollectiveId" != spm."CollectiveId" THEN 1
END) as "totalClaimed"
FROM "PaymentMethods" pm
LEFT JOIN "PaymentMethods" spm ON spm.id=pm."SourcePaymentMethodId"
WHERE pm.service='opencollective'
  AND pm.type='virtualcard'
GROUP by month
```

## Total amount donated using gift cards by issuer and currency

```
SELECT max(spmc.slug) as "gift card issuer", sum(amount) / 100 as "totalAmount", t.currency
FROM "Transactions" t
LEFT JOIN "PaymentMethods" pm ON t."PaymentMethodId" = pm.id
LEFT JOIN "PaymentMethods" spm ON pm."SourcePaymentMethodId" = spm.id
LEFT JOIN "Collectives" spmc ON spmc.id = spm."CollectiveId"
WHERE t.type = 'CREDIT'
  AND t."OrderId" IS NOT NULL
  AND pm.service = 'opencollective'
  AND pm.type = 'virtualcard'
GROUP BY spm."CollectiveId", t.currency
```

## Total number of Github contributors reached

This query will show a company how many unique contributors are participating to projects they have donated to through gift cards. Useful for companies that are donating to open-source as a way to promote their services to developers.

```sql
SELECT count(*) FROM (
    SELECT * FROM (
        SELECT json_object_keys((c.DATA ->> 'githubContributors')::json) AS contributors
        FROM "Transactions" t
        INNER JOIN "Collectives" c ON c.id = t."CollectiveId"
        WHERE t."UsingVirtualCardFromCollectiveId" = -- PUT THE COLLECTIVE ID HERE --
        AND t."type" = 'CREDIT'
        AND c.DATA IS NOT NULL
        AND c.DATA ->> 'githubContributors' IS NOT null
        GROUP BY c.id
    ) AS all_contributors
    GROUP BY contributors
) AS total_contributors
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://documentarians.gitbook.io/metadocumentation/master/internal/internal/queries/gift-cards.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
