# DuckDB 'PIVOT ON' SQL statement

**URL:** https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282
**Category:** Help
**Created:** [February 27, 2025, 8:23am UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282 "2025-02-27T08:23:27Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![stschiff](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/stschiff/32/9112_2.png) [@stschiff](https://talk.observablehq.com/u/stschiff)
#### Post date: [February 27, 2025, 8:23am UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/1 "2025-02-27T08:23:27Z")

</div>

Hi. I tried to use an SQL Pivot-statement (`PIVOT ... ON ... USING`) within an SQL Cell in a Notebook. I get `Error: Parser Error: syntax error at or near "PIVOT"`, and it looks like perhaps that Statement is not supported by the DuckDB-Wasm that is included in Notebooks?

I checked the [stdlib](https://github.com/observablehq/stdlib/blob/main/src/dependencies.js), which says that DuckDB-Wasm v 1.24 is included in Observable. I then checked with the [DuckDB-Wasm](https://duckdb.org/docs/clients/wasm/overview) documentation and the Browser demo-Prompt, which runs on v1.2.0 does in fact support PIVOT (I just hacked in the PIVOT-example from [here](https://duckdb.org/docs/sql/statements/pivot) and it works).

What’s a bit strange is that when I run `SELECT version() AS version;` inside an SQL cell in a notebook, I get `v0.0.1-dev0`, whereas in the [demo-Prompt](https://duckdb.org/docs/clients/wasm/overview) I get v1.2.0, so perhaps indeed the Notebook-DuckDB-Wasm-Version is somewhat outdated?

Any pointers?

---

<div class="post-metadata">

### Author: ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)
#### Post date: [February 27, 2025, 1:38pm UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/2 "2025-02-27T13:38:47Z")

</div>

We’re unfortunately still in the process of resolving an issue where range requests are incorrectly served from compressed files, which causes errors in Safari when file attachments are used with a newer version of DuckDB.

The reported version that you see is due to [PRAGMA version reports a wrong version in NPM version · Issue #1234 · duckdb/duckdb-wasm · GitHub](https://github.com/duckdb/duckdb-wasm/issues/1234)

---

<div class="post-metadata">

### Author: ![stschiff](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/stschiff/32/9112_2.png) [@stschiff](https://talk.observablehq.com/u/stschiff)
#### Post date: [February 28, 2025, 11:04am UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/3 "2025-02-28T11:04:09Z")

</div>

Ah OK. And what is the current version of DuckDB-Wasm included with Notebooks then?

---

<div class="post-metadata">

### Author: ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)
#### Post date: [February 28, 2025, 12:08pm UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/4 "2025-02-28T12:08:27Z")

</div>

It’s 1.24.0:

> <https://github.com/observablehq/stdlib/blob/v5.8.8/src/dependencies.js#L24>

---

<div class="post-metadata">

### Author: ![stschiff](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/stschiff/32/9112_2.png) [@stschiff](https://talk.observablehq.com/u/stschiff)
#### Post date: [March 1, 2025, 1:01pm UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/5 "2025-03-01T13:01:59Z")

</div>

OK, so then it is indeed 1.24, so I don’t get why the PIVOT command then doesn’t work… I tried this in Chrome, so I don’t see a link to the Safari error that you quote. Thanks.

---

<div class="post-metadata">

### Author: ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)
#### Post date: [March 1, 2025, 6:22pm UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/6 "2025-03-01T18:22:27Z")

</div>

Because duckdb-wasm versions don’t follow DuckDB. Version 1.24.0 bundles DuckDB version 0.7.1:

> **[duckdb-wasm/submodules at v1.24.0 · duckdb/duckdb-wasm](https://github.com/duckdb/duckdb-wasm/tree/v1.24.0/submodules)**
>
> WebAssembly version of DuckDB. Contribute to duckdb/duckdb-wasm development by creating an account on GitHub.

You can follow this issue for updates:

> <https://github.com/observablehq/feedback/issues/588>
>
> \*\*Is your feature request related to a problem? Please describe.\*\*
> I'm writing …a notebook that uses the native Observable DuckDB client. One query attempts using DuckDB's UNPIVOT statement introduced in DuckDB 0.8.0 (released around mid-May 2023, see \[announcement\](https://duckdb.org/2023/05/17/announcing-duckdb-080.html) and \[docs\](https://duckdb.org/docs/sql/statements/unpivot.html#unpivot-into-multiple-value-columns)). The query fails with a syntax error (see screenshot) which I'm guessing is due to an outdated duckdb-wasm version in Observable. Checking the stdlib \[dependencies file\](https://github.com/observablehq/stdlib/blob/main/src/dependencies.js) shows Observable is using version 1.24.0 of duckdb-wasm, which was published in March \[according to npm\](https://www.npmjs.com/package/@duckdb/duckdb-wasm?activeTab=versions). That months-long difference between 1.24.0 and 1.27.0 could explain why UNPIVOT isn't working within yet Observable.
> 
> \*\*Describe the solution you'd like\*\*
> Update to the latest version of duckdb-wasm, which at time of writing is 1.27.0 and was published mid-June.
> 
> \*\*Describe alternatives you've considered\*\*
> If updating isn't possible, one could update the intro notebook \["Hello, DuckDB"\](https://observablehq.com/@observablehq/duckdb) to mention what version of duckdb Observable is currently running so users can look at the correct documentation.
> 
> \*\*Additional context\*\*
> !\[unpivot syntax error\](https://github.com/observablehq/feedback/assets/7855609/aaa840e4-e154-4d7f-9f2c-d8ff9f5a2c1d)
> !\[stdlib dependencies\](https://github.com/observablehq/feedback/assets/7855609/af204603-4379-452f-ad54-8d230b859aa8)
> !\[npm duckdb-wasm versions\](https://github.com/observablehq/feedback/assets/7855609/a830a811-450c-4b53-8a1d-95dfe3031080)

---

<div class="post-metadata">

### Author: ![stschiff](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/stschiff/32/9112_2.png) [@stschiff](https://talk.observablehq.com/u/stschiff)
#### Post date: [March 3, 2025, 10:31am UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/7 "2025-03-03T10:31:52Z")

</div>

Aahh, OK, got it. Sorry, and thanks for pointing me to the GitHub issue on the matter. Very helpful!

---

<div class="post-metadata">

### Author: ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)
#### Post date: [March 3, 2025, 3:32pm UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/8 "2025-03-03T15:32:28Z")

</div>

@stschiff Would you mind giving this a try?

> **[DuckDBClient with DuckDB 1.1.1](https://observablehq.com/@mootari/duckdbclient)**
>
> Creates a patched \`DuckDBClient\` with duckdb-wasm 1.29.0 (duckdb 1.1.1). Usage: Example Implementation

Edit: I had messed up the original description - it’s duckdb-wasm 1.29.0 and duckdb 1.1.1.

---

<div class="post-metadata">

### Author: ![stschiff](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/stschiff/32/9112_2.png) [@stschiff](https://talk.observablehq.com/u/stschiff)
#### Post date: [March 5, 2025, 4:24pm UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/9 "2025-03-05T16:24:01Z")

</div>

Great. Your patch worked for me! I tested on Chrome and Safari, and it seemed to work on both browsers!

---

<div class="post-metadata">

### Author: ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)
#### Post date: [March 6, 2025, 10:05am UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/10 "2025-03-06T10:05:33Z")

</div>

Thanks for checking! I think the problem in Safari only shows itself with file attachments that get compressed at rest (which isn’t the case for every MIME type and requires a certain effective reduction in size).

---

<div class="post-metadata">

### Author: ![stschiff](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/stschiff/32/9112_2.png) [@stschiff](https://talk.observablehq.com/u/stschiff)
#### Post date: [October 7, 2025, 9:08pm UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/11 "2025-10-07T21:08:16Z")

</div>

Just a quick update here: I noticed that with your latest DuckCBClient, when I try to run: `db = DuckDBClient.of({query_data: FileAttachment(“my_file.tsv”)})` I get en error `db = Error: invalid source: [object Object]`. The same code works with the native (and old-version) DuckDBClient.

And this happens on both Chrome and Safari, so not sure whether it’s related to the issue you mentioned. I can try to make a minimal example, but I’m traveling the next days, so might take me a bit.

---

<div class="post-metadata">

### Author: ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)
#### Post date: [October 8, 2025, 9:34am UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/12 "2025-10-08T09:34:15Z")

</div>

@stschiff There was an `instanceof` check against `FileAttachment` that I’ve now replaced with a test of the constructor name. Can you give it another try?

---

<div class="post-metadata">

### Author: ![stschiff](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/stschiff/32/9112_2.png) [@stschiff](https://talk.observablehq.com/u/stschiff)
#### Post date: [October 21, 2025, 8:02am UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/13 "2025-10-21T08:02:09Z")

</div>

Hmm, with json data it doesn’t seem to work. See here: [https://observablehq.com/d/e524c4c8116e3af9](https://observablehq.com/d/e524c4c8116e3af9)

---

<div class="post-metadata">

### Author: ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)
#### Post date: [October 21, 2025, 2:27pm UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/14 "2025-10-21T14:27:09Z")

</div>

@stschiff I can’t access that.

---

<div class="post-metadata">

### Author: ![stschiff](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/stschiff/32/9112_2.png) [@stschiff](https://talk.observablehq.com/u/stschiff)
#### Post date: [October 21, 2025, 3:36pm UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/15 "2025-10-21T15:36:30Z")

</div>

Sorry, now you should be able to.

---

<div class="post-metadata">

### Author: ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)
#### Post date: [October 25, 2025, 9:19am UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/16 "2025-10-25T09:19:41Z")

</div>

You need to update your import. Imports from other workspaces are locked by default.

---

<div class="post-metadata">

### Author: ![stschiff](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/stschiff/32/9112_2.png) [@stschiff](https://talk.observablehq.com/u/stschiff)
#### Post date: [October 27, 2025, 7:04am UTC](https://talk.observablehq.com/t/duckdb-pivot-on-sql-statement/10282/17 "2025-10-27T07:04:58Z")

</div>

Aaaah, thank you. OK. Sorry, this was the first time I came across the import-locking feature. Very cool. And thanks for the continuous help! It works now.
