{"id":30320,"date":"2026-05-18T06:55:00","date_gmt":"2026-05-18T04:55:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=30320"},"modified":"2026-05-25T18:57:56","modified_gmt":"2026-05-25T16:57:56","slug":"sql-server-pivot-with-multiple-fixed-columns","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-pivot-with-multiple-fixed-columns\/","title":{"rendered":"SQL Server PIVOT with Multiple Fixed Columns: 1 Easy Query"},"content":{"rendered":"\n<p class=\"has-text-align-center\"><strong><em>A comprehensive article to use a SQL Server PIVOT with multiple fixed columns.<\/em><\/strong><\/p>\n\n\n\n<p>In a previous technical article, we used the PIVOT Operator in a query with one unique fixed column. This time it is a more complex use case, a T-SQL PIVOT with multiple fixed columns.<\/p>\n\n\n\n<p>Indeed, in this example, the table to pivot has 2 fixed non numeric columns. So the 2 columns will not be aggregated or calculated and they are also not pivoted.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-pivot-with-multiple-fixed-columns\/#1-create-a-sql-table-with-two-or-more-fixed-columns-to-pivot\" >1. Create a SQL table with two or more fixed columns to Pivot<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-pivot-with-multiple-fixed-columns\/#2-use-the-pivot-operator-to-manage-multiple-fixed-fields\" >2. Use the PIVOT operator to manage multiple fixed fields<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-pivot-with-multiple-fixed-columns\/#conclusion-about-the-t-sql-pivot-operator-with-fixed-data\" >Conclusion about the T-SQL PIVOT operator with fixed data<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-1-create-a-sql-table-with-two-or-more-fixed-columns-to-pivot\"><span class=\"ez-toc-section\" id=\"1-create-a-sql-table-with-two-or-more-fixed-columns-to-pivot\"><\/span>1. Create a SQL table with two or more fixed columns to Pivot<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here we use the same process as in the previous <a href=\"https:\/\/expert-only.com\/en\/ssis\/how-to-pivot-rows-to-columns-using-ssis\/\"><strong>article on the PIVOT operator.<\/strong><\/a> We simply group the data in a different way. Open <a href=\"https:\/\/expert-only.com\/en\/ssms\/download-ssms-18\/\"><strong>SSMS<\/strong><\/a> and execute this query to create the sample table and insert some data.<\/p>\n\n\n\n<p>Technically, we simply added the Customer ID and Customer Name as 2 new columns, with a NVARCHAR type.<\/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 the sales table with 2 additional columns\n-- The customer ID and the customer name\nCREATE table [dbo].[SALES_with_Customers] (\n    [MONTH]         NVARCHAR(20),\n    [CUSTOMER_ID]   NVARCHAR(20),\n    [CUSTOMER_NAME] NVARCHAR(20),\n    [AMOUNT]        NUMERIC(5)\n)\nGO\n\n-- Insert first sales amount for each month for Customer-001\nINSERT dbo.SALES_with_Customers VALUES ( N'January',  N'Customer-001', N'ONE', 1000)\nINSERT dbo.SALES_with_Customers VALUES ( N'February', N'Customer-001', N'ONE', 2000)\nINSERT dbo.SALES_with_Customers VALUES ( N'March',    N'Customer-001', N'ONE', 3000)\nINSERT dbo.SALES_with_Customers VALUES ( N'April',    N'Customer-001', N'ONE', 4000)\nINSERT dbo.SALES_with_Customers VALUES ( N'May',      N'Customer-001', N'ONE', 5000)\nINSERT dbo.SALES_with_Customers VALUES ( N'June',     N'Customer-001', N'ONE', 6000)\n-- second step for the first customer\nINSERT dbo.SALES_with_Customers VALUES ( N'January',  N'Customer-001', N'ONE', 1500)\nINSERT dbo.SALES_with_Customers VALUES ( N'February', N'Customer-001', N'ONE', 1500)\nINSERT dbo.SALES_with_Customers VALUES ( N'March',    N'Customer-001', N'ONE', 1500)\nINSERT dbo.SALES_with_Customers VALUES ( N'April',    N'Customer-001', N'ONE', 1500)\nINSERT dbo.SALES_with_Customers VALUES ( N'May',      N'Customer-001', N'ONE', 1500)\nINSERT dbo.SALES_with_Customers VALUES ( N'June',     N'Customer-001', N'ONE', 1500)\n\n-- Insert first sales amounts for each month for the Customer-002\nINSERT dbo.SALES_with_Customers VALUES ( N'January',  N'Customer-002', N'TWO', 1100)\nINSERT dbo.SALES_with_Customers VALUES ( N'February', N'Customer-002', N'TWO', 2200)\nINSERT dbo.SALES_with_Customers VALUES ( N'March',    N'Customer-002', N'TWO', 3300)\nINSERT dbo.SALES_with_Customers VALUES ( N'April',    N'Customer-002', N'TWO', 4400)\nINSERT dbo.SALES_with_Customers VALUES ( N'May',      N'Customer-002', N'TWO', 5500)\nINSERT dbo.SALES_with_Customers VALUES ( N'June',     N'Customer-002', N'TWO', 6600)\n-- Insert second sales amount for each month for Customer-002\nINSERT dbo.SALES_with_Customers VALUES ( N'January',  N'Customer-002', N'TWO', 2000)\nINSERT dbo.SALES_with_Customers VALUES ( N'February', N'Customer-002', N'TWO', 2000)\nINSERT dbo.SALES_with_Customers VALUES ( N'March',    N'Customer-002', N'TWO', 2000)\nINSERT dbo.SALES_with_Customers VALUES ( N'April',    N'Customer-002', N'TWO', 2000)\nINSERT dbo.SALES_with_Customers VALUES ( N'May',      N'Customer-002', N'TWO', 2000)\nINSERT dbo.SALES_with_Customers VALUES ( N'June',     N'Customer-002', N'TWO', 2000)\n\n-- Check inserted data\nSELECT \t*\nFROM \tdbo.[SALES_with_Customers];\n<\/pre>\n\n\n\n<p>Note that the source table contains 12 lines per Customer ID and Customer Name, for the 6 first months of the year.<\/p>\n\n\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"680\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/pivot-sql-server-multiple-columns-1-source-table-1.jpg\" alt=\"Create table in SSMS to perform SQL Server PIVOT with multiple fixed columns\" class=\"wp-image-17034\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/pivot-sql-server-multiple-columns-1-source-table-1.jpg 640w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/pivot-sql-server-multiple-columns-1-source-table-1-282x300.jpg 282w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\"><em><strong>Create table in SSMS to perform SQL Server PIVOT with multiple fixed columns<\/strong><\/em><\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-2-use-the-pivot-operator-to-manage-multiple-fixed-fields\"><span class=\"ez-toc-section\" id=\"2-use-the-pivot-operator-to-manage-multiple-fixed-fields\"><\/span>2. Use the PIVOT operator to manage multiple fixed fields<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Once the new table is created, build the pivot query. The exact method is used here.<\/p>\n\n\n\n<p>The only difference with <a href=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-pivot-rows-to-columns-in-sql-server\/\" data-type=\"post\" data-id=\"7598\">the simple PIVOT example<\/a> is that all the named columns are listed in the first select statement.<\/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=\"\">SELECT \t*\nFROM (\n    SELECT\n\t  [MONTH],\n\t  [CUSTOMER_ID],\n\t  [CUSTOMER_NAME],\n\t  [AMOUNT]\n    FROM dbo.SALES_with_Customers\n) AS Source_Table\n\nPIVOT (\n    AVG(AMOUNT)\n    FOR MONTH IN ([January], [February], [March], [April], [May], [June])\n) AS Pivot_Table;\n<\/pre>\n\n\n\n<p>Build the query using the same three steps.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>From the source table, select all the columns to pivot and to display:\n<ul class=\"wp-block-list\">\n<li><strong><em>Month, Customer_Id, Customer_Name and Amount<\/em><\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Configure the pivot column and the aggregation, here it&#8217;s <strong><em>AVG(AMOUNT)<\/em><\/strong><\/li>\n\n\n\n<li>Use the <strong><em>PIVOT operator<\/em><\/strong> to execute the transformation on the selected data.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"860\" height=\"460\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/pivot-sql-server-multiple-columns-2-build-query.jpg\" alt=\"Example of SQL Server PIVOT with multiple columns query explained \" class=\"wp-image-17040\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/pivot-sql-server-multiple-columns-2-build-query.jpg 860w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/pivot-sql-server-multiple-columns-2-build-query-300x160.jpg 300w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/pivot-sql-server-multiple-columns-2-build-query-768x411.jpg 768w\" sizes=\"auto, (max-width: 860px) 100vw, 860px\" \/><figcaption class=\"wp-element-caption\"><em><strong>Example of SQL Server PIVOT with multiple columns query explained <\/strong><\/em><\/figcaption><\/figure>\n<\/div>\n\n\n<p>Finally execute the query and check the calculated average of the sales column, displayed in the <em>Amount<\/em> column.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><em>The first customer&#8217;s data<\/em> is available in one unique line now.<\/li>\n\n\n\n<li>Same for <em>Customer-002<\/em>.<\/li>\n\n\n\n<li>Each month is now displayed in a <em>specific dedicated column<\/em>. <\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"740\" height=\"620\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/pivot-sql-server-multiple-columns-3-execute-query.jpg\" alt=\"Table before and after the PIVOT of the rows into columns with SQL Server\" class=\"wp-image-17058\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/pivot-sql-server-multiple-columns-3-execute-query.jpg 740w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/11\/pivot-sql-server-multiple-columns-3-execute-query-300x251.jpg 300w\" sizes=\"auto, (max-width: 740px) 100vw, 740px\" \/><figcaption class=\"wp-element-caption\"><em><strong>Table before and after the PIVOT of the rows into columns with SQL Server<\/strong><\/em><\/figcaption><\/figure>\n<\/div>\n\n\n<p>The ultimate goal of aggregation can be very different in the usage, it can be to use data for Reporting using one of the <a href=\"https:\/\/segment.com\/data-hub\/data-aggregation\/tools\/\" target=\"_blank\" rel=\"noreferrer noopener\">tools on the market<\/a>. But it can also be to limit the number of lines stored and have a better management of databases volumetry.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-about-the-t-sql-pivot-operator-with-fixed-data\"><span class=\"ez-toc-section\" id=\"conclusion-about-the-t-sql-pivot-operator-with-fixed-data\"><\/span>Conclusion about the T-SQL PIVOT operator with fixed data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In conclusion, this exploration of SQL Server PIVOT with multiple fixed columns significantly enhances our ability to manage and interpret complex datasets.<\/p>\n\n\n\n<p>By maintaining certain columns fixed while pivoting others, we unlock new dimensions of data analysis and reporting, showcasing the versatility and power of SQL Server for sophisticated data manipulation tasks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other resources on Pivot and Unpivot data operations using SQL and Excel<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-pivot-rows-to-columns-in-sql-server\/\">How to Pivot Rows To Columns in SQL Server ?<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/excel\/pivot-excel-table\/\">Tutorial to Pivot Excel table columns into rows.<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/excel\/create-excel-pivot-table\/\">How to create an Excel pivot table to analyse data?<\/a><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-sql-server-and-data-tutorials wp-block-embed-sql-server-and-data-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"h1kBkifKqT\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-pivot-rows-to-columns-in-sql-server\/\">Pivot Rows To Columns in SQL Server: Easy Query in 2 Steps<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;Pivot Rows To Columns in SQL Server: Easy Query in 2 Steps&#8221; &#8212; SQL Server and Data Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-pivot-rows-to-columns-in-sql-server\/embed\/#?secret=LUVdtZA5Fc#?secret=h1kBkifKqT\" data-secret=\"h1kBkifKqT\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>A comprehensive article to use a SQL Server PIVOT with multiple fixed columns. In a previous technical article, we used the PIVOT Operator in a query with one unique fixed column. This time it is a more complex use <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-pivot-with-multiple-fixed-columns\/\" title=\"SQL Server PIVOT with Multiple Fixed Columns: 1 Easy Query\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10314,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-30320","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/30320","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=30320"}],"version-history":[{"count":10,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/30320\/revisions"}],"predecessor-version":[{"id":31314,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/30320\/revisions\/31314"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10314"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=30320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=30320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=30320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}