{"id":29206,"date":"2023-11-13T06:22:00","date_gmt":"2023-11-13T05:22:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=29206"},"modified":"2023-11-15T16:03:08","modified_gmt":"2023-11-15T15:03:08","slug":"sql-server-dynamic-pivot-query","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/","title":{"rendered":"SQL Server Dynamic PIVOT Query"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\" id=\"h-script-example-to-build-a-dynamic-pivot-query-that-adapts-to-the-number-of-output-columns\"><em>Script example to build a dynamic pivot query that adapts to the number of output columns.<\/em><\/h4>\n\n\n\n<p>Writing a dynamic PIVOT query in SQL Server is a powerful way to transform and transpose data from rows into columns. Above all when the number of elements is not known beforehand or it changes regularly. Indeed, it is useful in scenarios where the data schema is dynamic or when generating reports and summaries from a variable dataset.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-why-use-a-dynamic-pivot-query-in-sql\">Why use a dynamic PIVOT query in SQL ?<\/h3>\n\n\n\n<p>A dynamic PIVOT query involves creating a SQL query string programmatically, which can pivot rows into an unknown number of columns. This is achieved by dynamically determining the unique values that will serve as the column headers in the pivoted table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-main-components-of-the-dynamic-pivot-query\">Main components of the dynamic PIVOT Query<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Static Columns<\/strong>: These are the columns in the data that will remain unchanged in the pivot transformation. The query dynamically accommodates these columns, allowing flexibility in selecting which columns to include.<\/li>\n\n\n\n<li><strong>Pivot Columns<\/strong>: The essence of the dynamic pivot lies in determining which columns are to be transformed. This part of the query dynamically identifies unique values in a specified column, which then become the headers of the new columns in the pivoted table.<\/li>\n\n\n\n<li><strong>Dynamic SQL Execution<\/strong>: The final step is the execution of the dynamically constructed SQL query. This is typically done using the <code>EXEC<\/code> or <code>sp_executesql<\/code> command in SQL Server.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-sql-server-dynamic-pivot-query-example\">SQL Server Dynamic PIVOT Query Example<\/h2>\n\n\n\n<p>Here is the SQL query to dynamically take the values from the column and build the PIVOT query statement. To go even further, it is also possible to add  variables to change the aggregation operator, the source table name and the source column.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- Step 1: declare the variables\nDECLARE\n   @StaticColumns     NVARCHAR(MAX),\n   @PivotColumns      NVARCHAR(MAX),\n   @DynamicPivotQuery NVARCHAR(MAX);\n\n-- Step 2: Identify the Static Columns\n-- Assuming Year and Month are our static columns, but you can modify this to add more\nSET @StaticColumns = N'Year, Month';\n\n-- Step 3: Identify the Dynamic Pivot Columns\nSELECT @PivotColumns = \n    STUFF((SELECT DISTINCT ',' + QUOTENAME(Product) \n           FROM SalesData \n           FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '');\n\n-- Step 4: Build the Dynamic Query\nSET @DynamicPivotQuery = \n  N'SELECT ' + @StaticColumns + ', ' + @PivotColumns + '\n    FROM (SELECT ' + @StaticColumns + ', Product, Amount FROM SalesData) AS SourceTable\n    PIVOT(SUM(Amount) FOR Product IN (' + @PivotColumns + ')) AS PivotTable';\n\n-- Step 5: Execute the Dynamic Query\nEXEC sp_executesql @DynamicPivotQuery;\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-result-overview-of-the-sql-query\">Result overview of the SQL Query<\/h3>\n\n\n\n<p>Here&#8217;s the table represented in a simple, non-HTML format:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>Year<\/th><th>Month<\/th><th>ProductA<\/th><th>ProductB<\/th><th>ProductC<\/th><th>ProductD<\/th><th>ProductE<\/th><\/tr><\/thead><tbody><tr><td>2020<\/td><td>1<\/td><td>100.00<\/td><td>160.00<\/td><td>NULL<\/td><td>130.00<\/td><td>190.00<\/td><\/tr><tr><td>2021<\/td><td>1<\/td><td>NULL<\/td><td>110.00<\/td><td>170.00<\/td><td>NULL<\/td><td>140.00<\/td><\/tr><tr><td>2022<\/td><td>1<\/td><td>150.00<\/td><td>NULL<\/td><td>120.00<\/td><td>180.00<\/td><td>NULL<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\"><em>Result of the dynamic SQL Server Pivot Query<\/em><\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-sql-ressources-to-use-in-the-query-example\">SQL Ressources to use in the query example<\/h4>\n\n\n\n<p>SQL code to create the sample table used in the code above:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE TABLE SalesData (\n    [Year]  INT,\n\t[Month] INT,\n    Product NVARCHAR(50),\n    Amount DECIMAL(10, 2)\n);\n<\/pre>\n\n\n\n<p>Insert statements to insert 20 rows in the table<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">INSERT INTO SalesData ([Year], [Month], Product, Amount) VALUES (2020, 1, 'ProductA', 100.00);\nINSERT INTO SalesData ([Year], [Month], Product, Amount) VALUES (2021, 1, 'ProductB', 110.00);\nINSERT INTO SalesData ([Year], [Month], Product, Amount) VALUES (2022, 1, 'ProductC', 120.00);\nINSERT INTO SalesData ([Year], [Month], Product, Amount) VALUES (2020, 1, 'ProductD', 130.00);\nINSERT INTO SalesData ([Year], [Month], Product, Amount) VALUES (2021, 1, 'ProductE', 140.00);\nINSERT INTO SalesData ([Year], [Month], Product, Amount) VALUES (2022, 1, 'ProductA', 150.00);\nINSERT INTO SalesData ([Year], [Month], Product, Amount) VALUES (2020, 1, 'ProductB', 160.00);\nINSERT INTO SalesData ([Year], [Month], Product, Amount) VALUES (2021, 1, 'ProductC', 170.00);\nINSERT INTO SalesData ([Year], [Month], Product, Amount) VALUES (2022, 1, 'ProductD', 180.00);\nINSERT INTO SalesData ([Year], [Month], Product, Amount) VALUES (2020, 1, 'ProductE', 190.00);\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-advantages-and-considerations\">Advantages and Considerations<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-advantages\">Advantages<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Flexibility<\/strong>: It offers the ability to <em>handle varying numbers of columns<\/em> without the need to modify the query for each specific case.<\/li>\n\n\n\n<li><strong>Scalability<\/strong>: Particularly useful in dealing with large and complex datasets where the schema may vary or evolve over time.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-considerations-and-risks\">Considerations and risks<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>SQL Injection Risk<\/strong>: As with any dynamic SQL, there&#8217;s an <em>inherent risk of SQL injection<\/em>. Proper validation and sanitization of inputs are crucial.<\/li>\n\n\n\n<li><strong>Performance<\/strong>: Dynamic queries can be more complex and potentially slower than static queries. Performance considerations should be taken into account, especially with large datasets.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-a-good-way-to-manage-changing-data-structures\">A good way to manage changing data structures<\/h3>\n\n\n\n<p>Using a dynamic PIVOT query in SQL Server is an really useful tool, especially for data transformation and <a href=\"https:\/\/expert-only.com\/en\/ssrs\/dynamic-sql-query-with-ssrs\/\"><strong>reporting.<\/strong><\/a> It provides a high degree of flexibility and adaptability for data analysis, making them a go-to technique for SQL Server developers and database <a href=\"https:\/\/www.computerscience.org\/careers\/database-administrator\/\">administrators<\/a>.<\/p>\n\n\n\n<p>We often face dynamic code in large ETL frameworks, where the integration T-SQL code is designed and reused in many components of an automated project. Of course before using dynamic code it is highly recommended to use static code and test it before making it scalable. For instance, here the static version of the <strong><a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-pivot-query-example\/\">SQL Server PIVOT query<\/a>.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/expert-only.com\/en\/t-sql\/sql-server-pivot-query-example\/\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Script example to build a dynamic pivot query that adapts to the number of output columns. Writing a dynamic PIVOT query in SQL Server is a powerful way to transform and transpose data from rows into columns. Above all <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/\" title=\"SQL Server Dynamic PIVOT Query\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10817,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-29206","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SQL Server Dynamic PIVOT Query - T-SQL<\/title>\n<meta name=\"description\" content=\"Example of a dynamic PIVOT query in SQL Server for flexible data transformation with a number of columns that changes overtime.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server Dynamic PIVOT Query\" \/>\n<meta property=\"og:description\" content=\"Example of a dynamic PIVOT query in SQL Server for flexible data transformation with a number of columns that changes overtime.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-13T05:22:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-15T15:03:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/planning-4897792_1920x1080.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"SQL Server Dynamic PIVOT Query\",\"datePublished\":\"2023-11-13T05:22:00+00:00\",\"dateModified\":\"2023-11-15T15:03:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/\"},\"wordCount\":559,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/planning-4897792_1920x1080.jpg\",\"articleSection\":[\"T-SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/\",\"url\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/\",\"name\":\"SQL Server Dynamic PIVOT Query - T-SQL\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/planning-4897792_1920x1080.jpg\",\"datePublished\":\"2023-11-13T05:22:00+00:00\",\"dateModified\":\"2023-11-15T15:03:08+00:00\",\"description\":\"Example of a dynamic PIVOT query in SQL Server for flexible data transformation with a number of columns that changes overtime.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/planning-4897792_1920x1080.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/planning-4897792_1920x1080.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server Dynamic PIVOT Query\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"SQL Server Dynamic PIVOT Query - T-SQL","description":"Example of a dynamic PIVOT query in SQL Server for flexible data transformation with a number of columns that changes overtime.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server Dynamic PIVOT Query","og_description":"Example of a dynamic PIVOT query in SQL Server for flexible data transformation with a number of columns that changes overtime.","og_url":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2023-11-13T05:22:00+00:00","article_modified_time":"2023-11-15T15:03:08+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/planning-4897792_1920x1080.jpg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"SQL Server Dynamic PIVOT Query","datePublished":"2023-11-13T05:22:00+00:00","dateModified":"2023-11-15T15:03:08+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/"},"wordCount":559,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/planning-4897792_1920x1080.jpg","articleSection":["T-SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/","url":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/","name":"SQL Server Dynamic PIVOT Query - T-SQL","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/planning-4897792_1920x1080.jpg","datePublished":"2023-11-13T05:22:00+00:00","dateModified":"2023-11-15T15:03:08+00:00","description":"Example of a dynamic PIVOT query in SQL Server for flexible data transformation with a number of columns that changes overtime.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/planning-4897792_1920x1080.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/planning-4897792_1920x1080.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-dynamic-pivot-query\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"SQL Server Dynamic PIVOT Query"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/29206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=29206"}],"version-history":[{"count":7,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/29206\/revisions"}],"predecessor-version":[{"id":29215,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/29206\/revisions\/29215"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10817"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=29206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=29206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=29206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}