Repository: duckdb/duckdb (language: python)

Issue:
Inconsistent results when using ORDER BY with PIVOT
### What happens?

PIVOT statement does not look to correctly handle ORDER BY clause.
Results sometimes sorted correctly and sometimes not. 
Running PIVOT multiple times produces different results

### To Reproduce

```sql
create table p (col1 timestamp, col2 int);
insert into p values ('2024-12-04 09:30:01', 100);
insert into p values ('2024-12-04 09:30:02', 100);
insert into p values ('2024-12-04 09:30:03', 100);
insert into p values ('2024-12-04 09:30:04', 100);

pivot p using sum (col2)
group by col1
order by col1;
```

### OS:

Windows

### DuckDB Version:

1.1.3

### DuckDB Client:

build from sourcce

### Hardware:

_No response_

### Full Name:

Alex G

### Affiliation:

Fintech

### What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have tested with a stable release

### Did you include all relevant data sets for reproducing the issue?

Yes

### Did you include all code required to reproduce the issue?

- [X] Yes, I have

### Did you include all relevant configuration (e.g., CPU architecture, Python version, Linux distribution) to reproduce the issue?

- [X] Yes, I have


Discussion / hints:

Fix patch:
diff --git a/src/parser/transform/statement/transform_pivot_stmt.cpp b/src/parser/transform/statement/transform_pivot_stmt.cpp
index 8b798a83c9fb..94bafd72f203 100644
--- a/src/parser/transform/statement/transform_pivot_stmt.cpp
+++ b/src/parser/transform/statement/transform_pivot_stmt.cpp
@@ -163,6 +163,8 @@ unique_ptr<QueryNode> Transformer::TransformPivotStatement(duckdb_libpgquery::PG
 		if (pivot->aggrs) {
 			TransformExpressionList(*pivot->aggrs, select_node->select_list);
 		}
+		// transform order by/limit modifiers
+		TransformModifiers(select, *select_node);
 		return std::move(select_node);
 	}
 


Test patch:
diff --git a/test/sql/pivot/pivot_15141.test b/test/sql/pivot/pivot_15141.test
new file mode 100644
index 000000000000..a08deced8197
--- /dev/null
+++ b/test/sql/pivot/pivot_15141.test
@@ -0,0 +1,32 @@
+# name: test/sql/pivot/pivot_15141.test
+# description: Test #15141: order by with no pivot columns
+# group: [pivot]
+
+statement ok
+PRAGMA enable_verification
+
+statement ok
+create table p (col1 timestamp, col2 int);
+
+statement ok
+INSERT INTO p VALUES 
+    ('2024-12-04 09:30:01', 100),
+    ('2024-12-04 09:30:02', 100),
+    ('2024-12-04 09:30:03', 100),
+    ('2024-12-04 09:30:04', 100),
+    ('2024-12-04 09:30:05', 100),
+    ('2024-12-04 09:30:06', 100),
+    ('2024-12-04 09:30:07', 100),
+    ('2024-12-04 09:30:08', 100);
+
+query II
+pivot p using sum (col2) group by col1 order by col1;
+----
+2024-12-04 09:30:01	100
+2024-12-04 09:30:02	100
+2024-12-04 09:30:03	100
+2024-12-04 09:30:04	100
+2024-12-04 09:30:05	100
+2024-12-04 09:30:06	100
+2024-12-04 09:30:07	100
+2024-12-04 09:30:08	100