SQLize
Online
/
PHPize Online
/
SQLtest Online
A
A
A
Share
Donate
Blog
Popular
Donate
A
A
A
Share
Blog
Popular
SQLize.online is a free online SQL environment for quickly running, experimenting with and sharing code.
You can run your SQL code on top of the most popular RDBMS including MySQL, MariaDB, SQLite, PostgreSQL, Oracle and Microsoft SQL Server.
SQL code:
Upload
Copy
Format
Clear
-- DROP FUNCTION public.f_pivot(text, text); CREATE OR REPLACE FUNCTION public.f_pivot(central_query text, headers_query text) RETURNS SETOF record LANGUAGE plpgsql VOLATILE AS $$ DECLARE left_column text; header_column text; value_column text; h_value text; headers_clause text := ''; query text; j json; r record; i int := 1; BEGIN -- find the column names of the source query EXECUTE 'SELECT row_to_json(_r.*) FROM (' || central_query || ') AS _r' INTO j; FOR r IN SELECT * FROM json_each_text(j) LOOP IF (i = 1) THEN left_column := r.key; ELSIF (i = 2) THEN header_column := r.key; ELSIF (i = 3) THEN value_column := r.key; END IF; i := i + 1; END LOOP; -- build the dynamic transposition query (based on the canonical model) FOR h_value IN EXECUTE headers_query LOOP headers_clause := concat(headers_clause, format(chr(10) || ', min(CASE WHEN %I = %L THEN %I::text END) AS %I', header_column, h_value, value_column, h_value )); END LOOP; query := format('SELECT %I %s FROM (SELECT *, row_number() OVER () AS rn FROM (%s) AS _c) AS _d GROUP BY %I ORDER BY min(rn)', left_column, headers_clause, central_query, left_column); -- Return the result set directly RETURN QUERY EXECUTE query; END; $$ EXECUTE ON ANY;
SQL
Server:
MariaDB 11.4
MariaDB 11.5
MariaDB 10
MariaDB 10 Sakila (ReadOnly)
MySQL 5.7
MySQL 5.7 Sakila (ReadOnly)
MySQL 8.0
MySQL 8.0 Sakila (ReadOnly)
SQLite 3
SQLite 3 Preloaded
PostgreSQL 10 Bookings (ReadOnly)
PostgreSQL 11
PostgreSQL 12
PostgreSQL 13
PostgreSQL 14
PostgreSQL 15
MS SQL Server 2017
MS SQL Server 2019
MS SQL Server 2022
MS SQL Server 2022 AdventureWorks (ReadOnly)
Firebird 4.0
Firebird 4.0 (Employee)
Oracle Database 19c (HR)
Oracle Database 21c
Oracle Database 23c Free
SOQOL
Version
ER Diagram
Preserve result
Stuck with a problem?
Got Error?
Ask ChatGPT!
Result:
Copy
Clear