# Poll: Export poll results in CSV for quiz creator?

**URL:** https://meta.discourse.org/t/poll-export-poll-results-in-csv-for-quiz-creator/94265
**Category:** Feature
**Created:** [8월 9, 2018, 12:05오전 UTC](https://meta.discourse.org/t/poll-export-poll-results-in-csv-for-quiz-creator/94265 "2018-08-09T00:05:48Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![JanJoost](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/janjoost/32/97173_2.png) [@JanJoost](https://meta.discourse.org/u/JanJoost)
#### Post date: [8월 9, 2018, 12:05오전 UTC](https://meta.discourse.org/t/poll-export-poll-results-in-csv-for-quiz-creator/94265/1 "2018-08-09T00:05:48Z")

</div>

Hi,

Would it be possible for the poll results to be exported to a CSV file or so?

Context: Users in my forum created a pub quiz (see [here](https://meta.discourse.org/t/how-discourse-enriches-our-community-fun-stuff/94207) if you’re interested to know more), but to tally the results one of the users is now going through the list of all answers, taking notes of who voted for which option. With 90 answers per question and 20 questions in a quiz, that’s a lot of tallying 🙂

Life would be much easier if there was a way to export the results in some way.

Or, alternatively, is there an easy option for me to use voters.json in a creative way?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [8월 9, 2018, 12:29오전 UTC](https://meta.discourse.org/t/poll-export-poll-results-in-csv-for-quiz-creator/94265/2 "2018-08-09T00:29:34Z")

</div>

You can use [data explorer](https://meta.discourse.org/t/32566?silent=true) for this, [Discourse Data Explorer](https://meta.discourse.org/t/data-explorer-plugin/32566)

Just need to author the right query and you should be good to go! I am sure someone in the community can help if we don’t already have it (search meta)

@rishabh one thing to think about with the [data explorer](https://meta.discourse.org/t/32566?silent=true) work is that we probably want to “ship” a bunch of useful queries longer term. Stuff like this is interesting.

---

<div class="post-metadata">

### Author: ![JanJoost](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/janjoost/32/97173_2.png) [@JanJoost](https://meta.discourse.org/u/JanJoost)
#### Post date: [8월 9, 2018, 8:22오후 UTC](https://meta.discourse.org/t/poll-export-poll-results-in-csv-for-quiz-creator/94265/3 "2018-08-09T20:22:21Z")

</div>

> [@sam](#):
>
> You can use [data explorer](https://meta.discourse.org/t/32566?silent=true) for this, [Data Explorer Plugin](https://meta.discourse.org/t/data-explorer-plugin/32566)

Hi @sam,

Thanks for the tip! I am not the most prolific Querybuilder in the world, but this at least got me started, so with some effort I managed to get the results! Not in the prettiest of ways, but hey - works for me!

Thanks again!

(For those interested: grabbed the json straight from the DB, converted that to CSV and processed that in a spreadsheet. Clumsy, I know, but I got there.)

---

<div class="post-metadata">

### Author: ![alefattorini](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alefattorini/32/119928_2.png) [@alefattorini](https://meta.discourse.org/u/alefattorini)
#### Post date: [12월 17, 2018, 9:36오전 UTC](https://meta.discourse.org/t/poll-export-poll-results-in-csv-for-quiz-creator/94265/4 "2018-12-17T09:36:06Z")

</div>

Could you please share with us your query?

---

<div class="post-metadata">

### Author: ![JanJoost](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/janjoost/32/97173_2.png) [@JanJoost](https://meta.discourse.org/u/JanJoost)
#### Post date: [12월 19, 2018, 10:50오전 UTC](https://meta.discourse.org/t/poll-export-poll-results-in-csv-for-quiz-creator/94265/5 "2018-12-19T10:50:55Z")

</div>

Hi there!

The new poll implementation changed stuff a bit, so I had to figure out how to get this sorted.

Please note that we’re sort of mis-using the poll-feature to build a quiz. That also means that we’re not really using the poll to identify the correct answer - all we want is an overview of the voting results, so we can export those to CSV and do some further manual processing to see who actually won the quiz.

Some assumptions:

- I know nothing about SQL. My querie can probably be optimised, rewritten to make eyes bleed less etc. But at least it works. For me. For now. In other words: Here be dragons.
- A quiz, the way our users built it, is nothing more than a topic with in that topic a post. And in that post the quizmaster creates a number of polls - one poll for each question.
- Closing a quiz means that the polls close. The quizmaster will then, after the polls close, publish the correct answers in another post. There is therefore no direct link between quiz questions and the correct answer, other than ‘read both posts’.

The workflow to run the quiz and collect the answers therefore is nothing more than:

1. Create quiz: Create topic, create post in topic with N poll objects, (one per question). Make sure to mark the results as public, and make sure to mark the polls as auto-close on a specific time and date (‘tonight at 22:00’).
2. Have people answer the questions by voting for the options.
3. As administrator, run the query to collect the raw answers. Create CSV, send over to quizmaster for further processing
4. As quizmaster: Remove all incorrect answers from spreadsheet, count the number of times each name of each contestant pops up. The name that comes up the most has the most correct answers and therefore wins the quiz.

Like I said: Fanatic mis-use of a great feature, and soooo many things in here that could make people cringe. However: I love the enthusiasm of the forum members - they came up with this all by themselves, and the quiz is very much enjoyed by all!

So, for the query: One of the asumptions is that there is one post in a topic which contains all the answers. The query requires you to get the topic id and the post number for the post containing the quiz. This can easily be found by clicking on the timestamp of the post in the upper left corner of the post (the one that says ‘Aug 9’ or ‘2d’ or ‘5m’ or so). That link will give you an url with somwhere at the end of the url the number of the topic followed by the post id.  
Take your post:  
`https://meta.discourse.org/t/poll-export-poll-results-in-csv-for-poll-creator/94265/4?u=janjoost`

See that bit at the end? The 94265/4? 94265 is the topic id, the 4 is the post number.

Then run the query below in [Data Explorer](https://meta.discourse.org/t/32566?silent=true), and get the results back in 4 columns:

- Poll name
- Poll answer option
- User ID for the forum member that chose this answer option
- Forum name of that member

[ninja-edit]: I forgot to mention that Discourse will translate the user id into the avatar of the user - when you download the results in CSV, you will get the actual user id and not the image.

As said: it ain’t pretty, but it works for me 🙂

Good luck!

```
-- [params]
-- int :topic_id
-- int :post_number

SELECT polls.name AS "Poll name", poll_options.html AS "Answer", poll_votes.user_id AS "User ID", users.username AS "Username"
	FROM poll_options
	INNER JOIN poll_votes ON poll_options.id=poll_votes.poll_option_id
	INNER JOIN polls ON polls.id=poll_votes.poll_id
	INNER JOIN users ON users.id=poll_votes.user_id
	WHERE poll_options.id IN (
		SELECT id FROM poll_options WHERE poll_options.poll_id IN (
			SELECT id FROM polls WHERE post_id IN (
				SELECT id FROM posts WHERE topic_id=:topic_id AND post_number=:post_number ) 
			)
		)
	ORDER BY polls.name, html

```

---

<div class="post-metadata">

### Author: ![torstensson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/torstensson/32/83638_2.png) [@torstensson](https://meta.discourse.org/u/torstensson)
#### Post date: [2월 18, 2019, 1:42오전 UTC](https://meta.discourse.org/t/poll-export-poll-results-in-csv-for-quiz-creator/94265/6 "2019-02-18T01:42:24Z")

</div>

A guy on my site just made a topic with multiple polls and with not showing who had voted, we were in quite a pickle to announce any winner. Your script worked perfectly and totally saved the day 🙂)

BIG thank you for sharing this @JanJoost!

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [11월 27, 2019, 1:51오전 UTC](https://meta.discourse.org/t/poll-export-poll-results-in-csv-for-quiz-creator/94265/7 "2019-11-27T01:51:36Z")

</div>

이제 투표에서 기본적으로 스태프에게 제공되도록 설정되었습니다:

 ![image](https://global.discourse-cdn.com/meta/original/3X/a/a/aa8af65419ca8f7b498ca5915bc4f6ce49e0ad56.png)

정상적으로 작동하려면 최신 버전의 Discourse와 [Data Explorer](https://meta.discourse.org/t/32566?silent=true) 플러그인이 활성화되어 있어야 합니다.

새로운 쿼리를 작성하고 설정에서 내보내기에 사용할 쿼리 ID를 변경하여 CSV 출력을 사용자 정의할 수도 있습니다.

---

<div class="post-metadata">

### Author: ![Kim\_Hanchet](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kim_hanchet/32/237875_2.png) [@Kim\_Hanchet](https://meta.discourse.org/u/Kim_Hanchet)
#### Post date: [5월 12, 2022, 5:34오후 UTC](https://meta.discourse.org/t/poll-export-poll-results-in-csv-for-quiz-creator/94265/8 "2022-05-12T17:34:33Z")

</div>

관리자가 결과를 내보낼 수 있나요?

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [5월 12, 2022, 5:56오후 UTC](https://meta.discourse.org/t/poll-export-poll-results-in-csv-for-quiz-creator/94265/9 "2022-05-12T17:56:20Z")

</div>

간단히 확인해 봤는데, 기본적으로 그렇게 할 수 없는 것 같고, 해당 기능을 활성화할 설정도 찾지 못했습니다.

다만, [data-explorer](https://meta.discourse.org/t/data-explorer-plugin/32566) 쿼리를 만들어 관리자 그룹에서 사용할 수 있게 하면 비슷한 작업을 수행할 수 있을지 모르겠습니다?

---

<div class="post-metadata">

### Author: ![Kim\_Hanchet](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kim_hanchet/32/237875_2.png) [@Kim\_Hanchet](https://meta.discourse.org/u/Kim_Hanchet)
#### Post date: [5월 12, 2022, 6:02오후 UTC](https://meta.discourse.org/t/poll-export-poll-results-in-csv-for-quiz-creator/94265/10 "2022-05-12T18:02:10Z")

</div>

> [@JanJoost](#):
>
> `post_number`

네, 그렇게 할게요! 좋은 설정이 될 것 같아요!

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [5월 12, 2022, 6:07오후 UTC](https://meta.discourse.org/t/poll-export-poll-results-in-csv-for-quiz-creator/94265/11 "2022-05-12T18:07:05Z")

</div>

자동으로 사용되는 것은 데이터 탐색기에서 `/admin/plugins/explorer?id=-16` 경로로 접근할 수 있지만, 게시글 ID(및 투표 이름)를 매개변수로 받아야 하므로 사용자 친화적이지는 않습니다. 그래도 이를 기반으로 무언가를 만들어 볼 수 있지 않을까요?

* * *

`topic_id`와 `post_number`(모든 게시글 주소에서 확인할 수 있는 값)를 기준으로 작동하는 간단한 투표 조회를 만들어 보았습니다. 이를 통해 해당 게시글에 포함된 모든 투표의 `post_id`와 투표 이름을 가져올 수 있습니다. 이렇게 하면 콘솔이나 JSON 파일을 직접 확인하지 않고도 기존 투표 쿼리에 이 정보를 전달하여 수동으로 실행할 수 있습니다. 👍

```plaintext
-- [params]
-- int :topic_id
-- int :post_number = 1

SELECT pl.post_id, 
       p.id,
       pl.name
FROM polls pl
JOIN posts p ON p.id = pl.post_id
WHERE p.topic_id = :topic_id
AND p.post_number = :post_number

```
