Use it when you want to begin data transformation work without writing the first draft from scratch.
Pivoting and Unpivoting Data AI Prompt
Write SQL to pivot rows to columns and unpivot columns to rows. Source data: {{data_description}} Desired output: {{desired_output}} Database: {{database}} 1. Manual pivot with... Copy this prompt template, run it in your AI tool, and use related prompts to continue the workflow.
Write SQL to pivot rows to columns and unpivot columns to rows.
Source data: {{data_description}}
Desired output: {{desired_output}}
Database: {{database}}
1. Manual pivot with CASE WHEN (works in all databases):
-- Pivot: rows of (product, month, revenue) → one column per month
SELECT
product,
SUM(CASE WHEN month = 'Jan' THEN revenue ELSE 0 END) AS jan_revenue,
SUM(CASE WHEN month = 'Feb' THEN revenue ELSE 0 END) AS feb_revenue,
SUM(CASE WHEN month = 'Mar' THEN revenue ELSE 0 END) AS mar_revenue
FROM monthly_sales
GROUP BY product;
2. CROSSTAB (PostgreSQL tablefunc extension):
CREATE EXTENSION IF NOT EXISTS tablefunc;
SELECT * FROM CROSSTAB(
'SELECT product, month, revenue
FROM monthly_sales
ORDER BY 1, 2',
'VALUES (''Jan''),(''Feb''),(''Mar'')'
) AS ct(product TEXT, jan NUMERIC, feb NUMERIC, mar NUMERIC);
3. Unpivot: columns to rows (PostgreSQL UNNEST approach):
-- Transform: one row with jan_rev, feb_rev, mar_rev → 3 rows
SELECT
product,
month_name,
revenue
FROM monthly_wide_table,
LATERAL (
VALUES
('Jan', jan_revenue),
('Feb', feb_revenue),
('Mar', mar_revenue)
) AS t(month_name, revenue);
4. Dynamic pivot (when column values are unknown at query time):
In SQL this requires dynamic SQL or a two-step approach:
Step 1: SELECT DISTINCT month FROM monthly_sales → get the list of columns
Step 2: Build and execute dynamic SQL with EXECUTE in a PL/pgSQL function
5. BigQuery / Snowflake PIVOT syntax:
-- BigQuery:
SELECT * FROM monthly_sales
PIVOT (SUM(revenue) FOR month IN ('Jan', 'Feb', 'Mar'));
-- Snowflake:
SELECT * FROM monthly_sales
PIVOT (SUM(revenue) FOR month IN ('Jan', 'Feb', 'Mar'))
AS p (product, jan, feb, mar);
Return: pivot SQL for the specific data, unpivot SQL if needed, and dynamic pivot approach if the column values are dynamic.When to use this prompt
Use it when you want a more consistent structure for AI output across projects or datasets.
Use it when you want prompt-driven work to turn into a reusable notebook or repeatable workflow later.
Use it when you want a clear next step into adjacent prompts in Data Transformation or the wider SQL Developer library.
What the AI should return
The AI should return a structured result that covers the main requested outputs, such as Manual pivot with CASE WHEN (works in all databases):, CROSSTAB (PostgreSQL tablefunc extension):, Unpivot: columns to rows (PostgreSQL UNNEST approach):. The final answer should stay clear, actionable, and easy to review inside a data transformation workflow for sql developer work.
How to use this prompt
Open your data context
Load your dataset, notebook, or working environment so the AI can operate on the actual project context.
Copy the prompt text
Use the copy button above and paste the prompt into the AI assistant or prompt input area.
Review the output critically
Check whether the result matches your data, assumptions, and desired format before moving on.
Chain into the next prompt
Once you have the first result, continue deeper with related prompts in Data Transformation.
Frequently asked questions
What does the Pivoting and Unpivoting Data prompt do?+
It gives you a structured data transformation starting point for sql developer work and helps you move faster without starting from a blank page.
Who is this prompt for?+
It is designed for sql developer workflows and marked as advanced, so it works well as a guided starting point for that level of experience.
What type of prompt is this?+
Pivoting and Unpivoting Data is a single prompt. You can copy it as-is, adapt it, or use it as one step inside a larger workflow.
Can I use this outside MLJAR Studio?+
Yes. The prompt text works in other AI tools too, but MLJAR Studio is the best fit when you want local execution, visible Python code, and reusable notebooks.
What should I open next?+
Natural next steps from here are Data Cleaning in SQL, String and Date Manipulation.