# SQL Question: Exporting to tab-delimited

**URL:** <https://forum.xojo.com/t/sql-question-exporting-to-tab-delimited/80064>\
**Category:** Databases\
**Created:** [April 22, 2024, 3:23pm UTC](https://forum.xojo.com/t/sql-question-exporting-to-tab-delimited/80064 "2024-04-22T15:23:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bob\_Gordon](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@Bob\_Gordon](https://forum.xojo.com/u/Bob_Gordon)\
**Post date:** [April 22, 2024, 3:23pm UTC](https://forum.xojo.com/t/sql-question-exporting-to-tab-delimited/80064/1 "2024-04-22T15:23:21Z")

</div>

Consider two tables:  
people  
answers

The people table contains name, email etc for a person in each row.  
The answers table contains answers for a person for questions. One answer per row. (There are at most five questions/answers.)

What I want to do is write out a tab-delimited file (so someone can play around with the data in Excel) where each line in the file has the person data (not a problem) and all the answers for that person.

The only way I have come up with to do this is while writing a line to the file do a query to get that person’s answers, assemble them into a string and stick them at the end of the line.

Anyone have any better ideas.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [April 22, 2024, 3:40pm UTC](https://forum.xojo.com/t/sql-question-exporting-to-tab-delimited/80064/2 "2024-04-22T15:40:34Z")

</div>

SQLite?

Normally you’d query with a `join` to get all the person/answer combinations. Or did I misunderstand the question?

---

<div class="post-metadata">

**Author:** ![Bob\_Gordon](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@Bob\_Gordon](https://forum.xojo.com/u/Bob_Gordon)\
**Post date:** [April 22, 2024, 6:55pm UTC](https://forum.xojo.com/t/sql-question-exporting-to-tab-delimited/80064/3 "2024-04-22T18:55:32Z")

</div>

I just did a query. I get something like

name1, answer1  
name1, answer2  
name1, answer3  
name2, answer1  
name2, answer2  
name2, answer3

What I want is:  
name1,answer1,answer2,answer3  
name2,answer1,answer2,answer3

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [April 22, 2024, 7:24pm UTC](https://forum.xojo.com/t/sql-question-exporting-to-tab-delimited/80064/4 "2024-04-22T19:24:21Z")

</div>

How about something like this?

```auto
select 
  name,
  '"' || group_concat( answer, '", "' ) || '"'
from
  person as p
  join answer as a on a.person_id = p.id
group by 1
;

```

---

<div class="post-metadata">

**Author:** ![Bob\_Gordon](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@Bob\_Gordon](https://forum.xojo.com/u/Bob_Gordon)\
**Post date:** [April 22, 2024, 10:47pm UTC](https://forum.xojo.com/t/sql-question-exporting-to-tab-delimited/80064/5 "2024-04-22T22:47:34Z")

</div>

I tried this. Problem: some people don’t have all the answers.

Thanks for the help.

Done for now.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [April 23, 2024, 2:19am UTC](https://forum.xojo.com/t/sql-question-exporting-to-tab-delimited/80064/6 "2024-04-23T02:19:52Z")

</div>

Use `LEFT JOIN` to get all the names even if there are no answers.

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [April 23, 2024, 5:56am UTC](https://forum.xojo.com/t/sql-question-exporting-to-tab-delimited/80064/7 "2024-04-23T05:56:39Z")

</div>

If this was a SQL Server or Oracle question, the answer would be in a SQL command called PIVOT.  
SQLite doesn’t have that command.

You need to do this with a set of LEFT OUTER joins, and making use of the COALESCE(,) function.  
This ensures that if someone has no answers, or a partial set of answers, that you always get _some_ response.

For example, if there is an answer row where the Question is ‘CITY’, you need to create one query that returns CITY for everyone.

**Select PERSON\_ID, COALESCE(Answer, " ") as CITY from ANSWERS;**

If you have 3 people, that might return

**1, “Chicago”**  
**3, “Boston”**

note that person 2 has no answer at all.  
Save the query / view as qryCITY

Now, you repeat that for TOWN, and all the other answer fields

========

Finally, you wrap them all up using a combination query

> SELECT a.PERSON\_ID,  
> a.SURNAME,  
> a.FORENAME,  
> COALESCE(b.CITY, " “) CITY,  
> COALESCE(c.TOWN,” ") TOWN  
> from  
> **PEOPLE a** LEFT OUTER JOIN **qryCITY b**  
> on a.PERSON\_ID = b.PERSON\_ID  
> LEFT OUTER JOIN **qryTOWN c**  
> on a.PERSON\_ID = c.PERSON\_ID;

Now, all of that is typed straight from my head to the page, as I don’t have a SQL test bed set up at the moment,  
but hopefully that will get you where you need to go.

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [April 23, 2024, 1:12pm UTC](https://forum.xojo.com/t/sql-question-exporting-to-tab-delimited/80064/8 "2024-04-23T13:12:55Z")

</div>

> [@Bob\_Gordon](#):
>
> What I want is:  
> name1,answer1,answer2,answer3  
> name2,answer1,answer2,answer3

It looks like you want to ‘pivot’ your query. (Jeff confirm this information above, I typed this yesterday)  
There is an extension for sqlite: [GitHub - jakethaw/pivot\_vtab: SQLite3 pivot virtual table](https://github.com/jakethaw/pivot_vtab)  
There are tools that offer pivot like: [PIVOT Statement - DuckDB](https://duckdb.org/docs/sql/statements/pivot.html)  
Maybe MBS offers the pivot extension.  
You can open a feature request to add the extension to Xojo.
