# Transpose select results rows into columns

**URL:** <https://community.stardog.com/t/transpose-select-results-rows-into-columns/1971>\
**Category:** Support\
**Created:** [October 3, 2019, 3:58pm UTC](https://community.stardog.com/t/transpose-select-results-rows-into-columns/1971 "2019-10-03T15:58:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![R3000](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/r3000/32/358_2.png) [@R3000](https://community.stardog.com/u/R3000)\
**Post date:** [October 3, 2019, 3:58pm UTC](https://community.stardog.com/t/transpose-select-results-rows-into-columns/1971/1 "2019-10-03T15:58:46Z")

</div>

Ok, here is another Tableau developer request. The sparql select query returns just four columns and and lots of rows. The last two columns are called key and value - which is actually a property (such as book:title, book:description) and object (such as "A book title", "a sample book description"). I have lots of properties so I am am trying to avoid a very long sparql query.

- How do I re-write the sparql query without including variable for each property type?  
(I understand that such sparql query will return lots of NULLS wich Tableau is ok with)

If my question is not clear enough I can provide an example.

BTW:

- What is the Stardog limitation of sparql query length?

Re gads,  
Radu

---

<div class="post-metadata">

**Author:** ![R3000](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/r3000/32/358_2.png) [@R3000](https://community.stardog.com/u/R3000)\
**Post date:** [October 3, 2019, 4:58pm UTC](https://community.stardog.com/t/transpose-select-results-rows-into-columns/1971/2 "2019-10-03T16:58:01Z")

</div>

Thinking aloud now. What if we use the following as a subquery that feeds into main query:

```auto
select distinct ?p where {?s ?p ?o}

```

The above returns a list of distinct properties.

---

<div class="post-metadata">

**Author:** ![Katherine\_Studzinski](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/katherine_studzinski/32/530_2.png) [@Katherine\_Studzinski](https://community.stardog.com/u/Katherine_Studzinski)\
**Post date:** [October 3, 2019, 5:29pm UTC](https://community.stardog.com/t/transpose-select-results-rows-into-columns/1971/3 "2019-10-03T17:29:57Z")

</div>

> [@R3000](#):
>
> If my question is not clear enough I can provide an example.

Hi Radu,

Could you please provide an example?

Thank you!

---

<div class="post-metadata">

**Author:** ![lorenz\_b](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@lorenz\_b](https://community.stardog.com/u/lorenz_b)\
**Post date:** [October 5, 2019, 7:19am UTC](https://community.stardog.com/t/transpose-select-results-rows-into-columns/1971/4 "2019-10-05T07:19:36Z")

</div>

Then you still just have the same columns as before, just a column `p` in the final result.

What I understand, you have lets say 3 columns, first column `s` denoting the entity and two columns `c1` and `c2` for key and value

```
+----+----+----+
| s | c1 | c2 |
+----+----+----+
| s1 | p1 | v1 |
| s1 | p1 | v2 |
| s1 | p2 | v3 |
| s2 | p1 | v4 |
| .. | .. | .. |
+----+----+----+

```

and you want

```
+----+----+----+----+----+
| s | p1 | p2 | .. | pn |
+----+----+----+----+----+
| s1 | v1 | v3 | | |
| s1 | v1 | v3 | | |
| s2 | v4 | | | |
| .. | .. | .. | | |
+----+----+----+----+----+

```

If that's not what you meant, please provide an example.

If that is a correct example, short answer: You can't create columns from variables in SPARQL. That is impossible, at least in standard SPARQL. All columns have to be specified in the projection part of a query.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [October 19, 2019, 7:19am UTC](https://community.stardog.com/t/transpose-select-results-rows-into-columns/1971/5 "2019-10-19T07:19:38Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
